From 4f71eb5c419a520551eace0e668e2cfe5f99a954 Mon Sep 17 00:00:00 2001 From: Paul Higinbotham Date: Tue, 24 Mar 2020 10:36:07 -0700 Subject: [PATCH] Fix foreach parallel when current drive is not available --- .../engine/hostifaces/PSTask.cs | 4 ++++ .../Foreach-Object-Parallel.Tests.ps1 | 14 ++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/src/System.Management.Automation/engine/hostifaces/PSTask.cs b/src/System.Management.Automation/engine/hostifaces/PSTask.cs index 45b3e944917..a4bc49a7af2 100644 --- a/src/System.Management.Automation/engine/hostifaces/PSTask.cs +++ b/src/System.Management.Automation/engine/hostifaces/PSTask.cs @@ -449,6 +449,10 @@ public void Start(Runspace runspace) Runspace.DefaultRunspace = runspace; runspace.ExecutionContext.SessionState.Internal.SetLocation(_currentLocationPath); } + catch (DriveNotFoundException) + { + // Allow task to run if current drive is not available. + } finally { Runspace.DefaultRunspace = oldDefaultRunspace; diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Foreach-Object-Parallel.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Foreach-Object-Parallel.Tests.ps1 index 2e6c5404fde..63b6236b756 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Foreach-Object-Parallel.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Foreach-Object-Parallel.Tests.ps1 @@ -104,6 +104,20 @@ Describe 'ForEach-Object -Parallel Basic Tests' -Tags 'CI' { $parallelScriptLocation = 1..1 | ForEach-Object -Parallel { $PWD } $parallelScriptLocation.Path | Should -BeExactly $PWD.Path } + + It 'Verifies no terminating error if current working drive is not found' { + $oldLocation = Get-Location + try + { + New-PSDrive -Name ZZ -PSProvider FileSystem -Root $TestDrive + Set-Location -Path 'ZZ:' + { 1..1 | ForEach-Object -Parallel { $_ } } | Should -Not -Throw + } + finally + { + Set-Location -Path $oldLocation + } + } } Describe 'ForEach-Object -Parallel common parameters' -Tags 'CI' {