Skip to content

Unexpected behavior of the new .CommandLine ETS script property on Process objects on Linux #13944

@mklement0

Description

@mklement0

Sometime after 7.0, a new ETS script property named CommandLine was introduced for System.Diagnostics.Process instances, so that you could query a given process' command line.; e.g.:

# Currently Windows and Linux only: get the PowerShell session's own command line.
(Get-Process -Id $PID).CommandLine

As an aside: The property doesn't yet support macOS - see #13943.

On Linux, this is in effect implemented via the following command, using Linux' special files:

Get-Content /proc/$PID/cmdline 

The problem is that the content of these files contains the verbatim arguments as originally used during process creation separated with NUL characters, which in the terminal - due to NUL getting ignored - presents like directly joined arguments, which is unhelpful to the human observer.

The question is how much effort we want to expend to present a functional command line that is the equivalent of the original:

  • A simple solution would be to simply space-concatenate the verbatim arguments -
    (Get-Content /proc/$PID/cmdline) -replace "`0", ' ' - for a friendlier presentation:

  • Trying to recreate a functional command line would require substantially more effort - namely for recreating required quoting and escaping - and has an inherent conceptual problem:

Steps to reproduce

On Linux, from PowerShell"

pwsh -noprofile -command 'Get-Content /proc/$PID/cmdline'

Expected behavior

A string with the following verbatim content:

/usr/bin/pwsh -noprofile -command Get-Content /proc/$PID/cmdline

This assumes simple space concatenation without any attempt to recreated required quoting and escaping; see discussion above.

Actual behavior

Since the verbatim arguments are NUL-concatenated, they appear as directly concatenated; note the lack of spaces between pwsh, -noprofile, -command, and the latter's argument:

/usr/bin/pwsh-noprofile-commandGet-Content /proc/$PID/cmdline

Environment data

PowerShell Core 7.1.0-rc.2

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-BugIssue has been identified as a bug in the productResolution-FixedThe issue is fixed.Up-for-GrabsUp-for-grabs issues are not high priorities, and may be opportunities for external contributorsWG-Enginecore PowerShell engine, interpreter, and runtimeWaiting - DotNetCorewaiting on a fix/change in .NET

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions