From 1c4b98cb7ce0c029f0ce63df8bc2344bf09d78de Mon Sep 17 00:00:00 2001 From: Paul Higinbotham Date: Mon, 11 Nov 2019 14:54:21 -0800 Subject: [PATCH 1/4] Fix Start-Job working directory with trailing back slash --- .../engine/hostifaces/PowerShellProcessInstance.cs | 2 +- test/powershell/engine/Job/Jobs.Tests.ps1 | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/System.Management.Automation/engine/hostifaces/PowerShellProcessInstance.cs b/src/System.Management.Automation/engine/hostifaces/PowerShellProcessInstance.cs index f0f71454630..4dc62e0116b 100644 --- a/src/System.Management.Automation/engine/hostifaces/PowerShellProcessInstance.cs +++ b/src/System.Management.Automation/engine/hostifaces/PowerShellProcessInstance.cs @@ -58,7 +58,7 @@ public PowerShellProcessInstance(Version powerShellVersion, PSCredential credent CultureInfo.InvariantCulture, "{0} -wd \"{1}\"", processArguments, - workingDirectory.Replace("\"", "\"\"")); + workingDirectory.Replace("\"", "\"\"").Replace("\\", "\\\\")); } if (initializationScript != null) diff --git a/test/powershell/engine/Job/Jobs.Tests.ps1 b/test/powershell/engine/Job/Jobs.Tests.ps1 index d9d2999133a..d67fb976062 100644 --- a/test/powershell/engine/Job/Jobs.Tests.ps1 +++ b/test/powershell/engine/Job/Jobs.Tests.ps1 @@ -94,6 +94,17 @@ Describe 'Basic Job Tests' -Tags 'CI' { $jobOutput | Should -BeExactly $path.ToString() } + It 'Verifies the working directory parameter path with trailing backslash' -Skip:(! $IsWindows) { + $path = 'C:\' + if (! (Test-Path -Path $path)) + { + Write-Warning "Test skipped because there is no prerequisite 'C:\' file system root." + return + } + $location = Start-Job { $pwd } -WorkingDirectory $path | Receive-Job -Wait -AutoRemoveJob + $location.Path | Should -BeExactly $path + } + It 'Throws an error when the working directory parameter is ' -TestCases $invalidPathTestCases { param($path, $case, $expectedErrorId) From e1d29dc08d6fc83d80bfad9741cd59fab063267d Mon Sep 17 00:00:00 2001 From: Paul Higinbotham Date: Tue, 12 Nov 2019 08:29:49 -0800 Subject: [PATCH 2/4] CR feedback. --- test/powershell/engine/Job/Jobs.Tests.ps1 | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/test/powershell/engine/Job/Jobs.Tests.ps1 b/test/powershell/engine/Job/Jobs.Tests.ps1 index d67fb976062..c58a938ef53 100644 --- a/test/powershell/engine/Job/Jobs.Tests.ps1 +++ b/test/powershell/engine/Job/Jobs.Tests.ps1 @@ -95,14 +95,8 @@ Describe 'Basic Job Tests' -Tags 'CI' { } It 'Verifies the working directory parameter path with trailing backslash' -Skip:(! $IsWindows) { - $path = 'C:\' - if (! (Test-Path -Path $path)) - { - Write-Warning "Test skipped because there is no prerequisite 'C:\' file system root." - return - } - $location = Start-Job { $pwd } -WorkingDirectory $path | Receive-Job -Wait -AutoRemoveJob - $location.Path | Should -BeExactly $path + $job = Start-Job { $pwd } -WorkingDirectory '\' | Wait-Job + $job.JobStateInfo.State | Should -BeExactly 'Completed' } It 'Throws an error when the working directory parameter is ' -TestCases $invalidPathTestCases { From 9f81dd556b57936a65e3b52aab9fb494bef3a6d3 Mon Sep 17 00:00:00 2001 From: Paul Higinbotham Date: Wed, 13 Nov 2019 10:00:49 -0800 Subject: [PATCH 3/4] Use StartInfo.ArgumentList for start-job process arguments. --- .../hostifaces/PowerShellProcessInstance.cs | 48 ++++++++----------- 1 file changed, 21 insertions(+), 27 deletions(-) diff --git a/src/System.Management.Automation/engine/hostifaces/PowerShellProcessInstance.cs b/src/System.Management.Automation/engine/hostifaces/PowerShellProcessInstance.cs index 4dc62e0116b..dc4a16bbd87 100644 --- a/src/System.Management.Automation/engine/hostifaces/PowerShellProcessInstance.cs +++ b/src/System.Management.Automation/engine/hostifaces/PowerShellProcessInstance.cs @@ -50,38 +50,11 @@ static PowerShellProcessInstance() /// Specifies the initial working directory for the new powershell process. public PowerShellProcessInstance(Version powerShellVersion, PSCredential credential, ScriptBlock initializationScript, bool useWow64, string workingDirectory) { - string processArguments = " -s -NoLogo -NoProfile"; - - if (!string.IsNullOrWhiteSpace(workingDirectory)) - { - processArguments = string.Format( - CultureInfo.InvariantCulture, - "{0} -wd \"{1}\"", - processArguments, - workingDirectory.Replace("\"", "\"\"").Replace("\\", "\\\\")); - } - - if (initializationScript != null) - { - string scripBlockAsString = initializationScript.ToString(); - if (!string.IsNullOrEmpty(scripBlockAsString)) - { - string encodedCommand = - Convert.ToBase64String(Encoding.Unicode.GetBytes(scripBlockAsString)); - processArguments = string.Format( - CultureInfo.InvariantCulture, - "{0} -EncodedCommand {1}", - processArguments, - encodedCommand); - } - } - // 'WindowStyle' is used only if 'UseShellExecute' is 'true'. Since 'UseShellExecute' is set // to 'false' in our use, we can ignore the 'WindowStyle' setting in the initialization below. _startInfo = new ProcessStartInfo { FileName = PwshExePath, - Arguments = processArguments, UseShellExecute = false, RedirectStandardInput = true, RedirectStandardOutput = true, @@ -92,6 +65,27 @@ public PowerShellProcessInstance(Version powerShellVersion, PSCredential credent #endif }; + _startInfo.ArgumentList.Add("-s"); + _startInfo.ArgumentList.Add("-NoLogo"); + _startInfo.ArgumentList.Add("-NoProfile"); + + if (!string.IsNullOrWhiteSpace(workingDirectory)) + { + _startInfo.ArgumentList.Add("-wd"); + _startInfo.ArgumentList.Add(workingDirectory.Replace("\"", "\"\"")); + } + + if (initializationScript != null) + { + var scriptBlockString = initializationScript.ToString(); + if (!string.IsNullOrEmpty(scriptBlockString)) + { + var encodedCommand = Convert.ToBase64String(Encoding.Unicode.GetBytes(scriptBlockString)); + _startInfo.ArgumentList.Add("-EncodedCommand"); + _startInfo.ArgumentList.Add(encodedCommand); + } + } + if (credential != null) { Net.NetworkCredential netCredential = credential.GetNetworkCredential(); From 46be165be0b78012940c6d7a0c38eaf01e1482a6 Mon Sep 17 00:00:00 2001 From: Paul Higinbotham Date: Wed, 13 Nov 2019 12:50:00 -0800 Subject: [PATCH 4/4] Remove unneeded quote escaping --- .../engine/hostifaces/PowerShellProcessInstance.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/System.Management.Automation/engine/hostifaces/PowerShellProcessInstance.cs b/src/System.Management.Automation/engine/hostifaces/PowerShellProcessInstance.cs index dc4a16bbd87..862b42297e8 100644 --- a/src/System.Management.Automation/engine/hostifaces/PowerShellProcessInstance.cs +++ b/src/System.Management.Automation/engine/hostifaces/PowerShellProcessInstance.cs @@ -72,7 +72,7 @@ public PowerShellProcessInstance(Version powerShellVersion, PSCredential credent if (!string.IsNullOrWhiteSpace(workingDirectory)) { _startInfo.ArgumentList.Add("-wd"); - _startInfo.ArgumentList.Add(workingDirectory.Replace("\"", "\"\"")); + _startInfo.ArgumentList.Add(workingDirectory); } if (initializationScript != null)