Fix Start-Job working directory path with trailing back slash (#11041)

This commit is contained in:
Paul Higinbotham
2019-11-15 17:05:27 -08:00
committed by Aditya Patwardhan
parent 8ff29ddc62
commit bd0e30dec1
2 changed files with 26 additions and 27 deletions
@@ -50,38 +50,11 @@ namespace System.Management.Automation.Runspaces
/// <param name="workingDirectory">Specifies the initial working directory for the new powershell process.</param>
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("\"", "\"\""));
}
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 @@ namespace System.Management.Automation.Runspaces
#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);
}
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();
@@ -94,6 +94,11 @@ Describe 'Basic Job Tests' -Tags 'CI' {
$jobOutput | Should -BeExactly $path.ToString()
}
It 'Verifies the working directory parameter path with trailing backslash' -Skip:(! $IsWindows) {
$job = Start-Job { $pwd } -WorkingDirectory '\' | Wait-Job
$job.JobStateInfo.State | Should -BeExactly 'Completed'
}
It 'Throws an error when the working directory parameter is <case>' -TestCases $invalidPathTestCases {
param($path, $case, $expectedErrorId)