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 @@ -484,7 +484,7 @@ public virtual ScriptBlock InitializationScript
/// Gets or sets an initial working directory for the powershell background job.
/// </summary>
[Parameter]
[ValidateNotNullOrEmpty]
[ValidateNotNullOrWhiteSpace]
public string WorkingDirectory { get; set; }

/// <summary>
Expand Down Expand Up @@ -601,7 +601,7 @@ protected override void BeginProcessing()
ThrowTerminatingError(errorRecord);
}

if (WorkingDirectory != null && !Directory.Exists(WorkingDirectory))
if (WorkingDirectory != null && !InvokeProvider.Item.IsContainer(WorkingDirectory))
{
string message = StringUtil.Format(RemotingErrorIdStrings.StartJobWorkingDirectoryNotFound, WorkingDirectory);
var errorRecord = new ErrorRecord(
Expand Down
15 changes: 13 additions & 2 deletions test/powershell/engine/Job/Jobs.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,17 @@ Describe 'Basic Job Tests' -Tags 'Feature' {
$jobOutput | Should -BeExactly $path.ToString()
}

It 'Can specify the working directory with a PSPath' {
try {
Push-Location 'Temp:\'
$job = Start-Job -ScriptBlock { $PWD } -WorkingDirectory $pwd.Path | Wait-Job
$jobOutput = Receive-Job $job
$jobOutput | Should -BeExactly $pwd.Path
} finally {
Pop-Location
}
}

It 'Can use the user specified working directory parameter with quote' -Skip:($IsWindows) {
$path = Join-Path -Path $TestDrive -ChildPath "My ""Dir"
$null = New-Item -ItemType Directory -Path "$path"
Expand All @@ -100,9 +111,9 @@ Describe 'Basic Job Tests' -Tags 'Feature' {
}

It 'Throws an error when the working directory parameter is <case>' -TestCases $invalidPathTestCases {
param($path, $case, $expectedErrorId)
param($path, $case, $errorId)

{Start-Job -ScriptBlock { 1 + 1 } -WorkingDirectory $path} | Should -Throw -ErrorId $expectedErrorId
{Start-Job -ScriptBlock { 1 + 1 } -WorkingDirectory $path} | Should -Throw -ErrorId $errorId
}

It 'Verifies that the current working directory is preserved' {
Expand Down