-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Description
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:- This results in loss of information: the original argument boundaries aren't preserved.
- However, it may be good enough for the human observer, and it is what the standard
ps
utility does with-o command=
, which is the only similarly simple solution for macOS - see Make the .CommandLine ETS script property on Process objects work on macOS too #13943.
-
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:
- Given that PowerShell syntax differs from that of POSIX-compatible shells: which shell's syntax do you use for the reconstruction? Arguably, it should be PowerShell's (which, of course, is still fundamentally hampered by Arguments for external executables aren't correctly escaped #1995).
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