Fix the process CommandLine on Linux (#18710)

This commit is contained in:
Jordan Borean
2022-12-02 10:23:00 -08:00
committed by GitHub
parent 11ffaf1e33
commit 6fa7260db9
2 changed files with 21 additions and 4 deletions
@@ -1164,7 +1164,8 @@ namespace System.Management.Automation.Runspaces
if ($IsWindows) {
(Get-CimInstance Win32_Process -Filter ""ProcessId = $($this.Id)"").CommandLine
} elseif ($IsLinux) {
Get-Content -LiteralPath ""/proc/$($this.Id)/cmdline""
$rawCmd = Get-Content -LiteralPath ""/proc/$($this.Id)/cmdline""
$rawCmd.Substring(0, $rawCmd.Length - 1) -replace ""`0"", "" ""
}
"),
setterScript: null,
@@ -96,9 +96,25 @@ Describe "Get-Process" -Tags "CI" {
}
It "Should return CommandLine property" -Skip:($IsMacOS) {
$command = "(Get-Process -Id `$pid).CommandLine"
$result = & "$PSHOME/pwsh" -NoProfile -NonInteractive -Command $command
$result | Should -BeLike "*$command*"
if ($IsWindows) {
# Windows will convert the bound parameters and quote them if it
# contains whitespace. Any inner double quotes are escaped with \".
$expected = "`"$PSHOME\pwsh.exe`" -NoProfile -NoLogo -NonInteractive -Command `"(Get-Process -Id \`"`$pid\`").CommandLine`""
$actual = & {
$PSNativeCommandArgumentPassing = 'Windows'
& $PSHOME/pwsh -NoProfile -NoLogo -NonInteractive -Command '(Get-Process -Id "$pid").CommandLine'
}
} else {
# Linux passes arguments as they are bound. As there is no actual
# command line string, pwsh just joins each array with a space
# without attempting to use some sort of quoting rule.
$expected = "$PSHOME/pwsh -NoProfile -NoLogo -NonInteractive -Command (Get-Process -Id `"`$pid`").CommandLine"
$actual = & "$PSHOME/pwsh" -NoProfile -NoLogo -NonInteractive -Command '(Get-Process -Id "$pid").CommandLine'
}
$actual | Should -Be $expected
}
}