diff --git a/src/System.Management.Automation/engine/TypeTable_Types_Ps1Xml.cs b/src/System.Management.Automation/engine/TypeTable_Types_Ps1Xml.cs index 5ecd591490..22bdd8bcd3 100644 --- a/src/System.Management.Automation/engine/TypeTable_Types_Ps1Xml.cs +++ b/src/System.Management.Automation/engine/TypeTable_Types_Ps1Xml.cs @@ -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, diff --git a/test/powershell/Modules/Microsoft.PowerShell.Management/Get-Process.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Management/Get-Process.Tests.ps1 index 79b592f31f..4cde8baefe 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Management/Get-Process.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Management/Get-Process.Tests.ps1 @@ -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 } }