Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -50,38 +50,11 @@ static PowerShellProcessInstance()
/// <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,
Expand All @@ -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);
}

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();
Expand Down
5 changes: 5 additions & 0 deletions test/powershell/engine/Job/Jobs.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down