diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetHash.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetHash.cs index 78017d46db2..7afca70e3fe 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetHash.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetHash.cs @@ -156,6 +156,15 @@ protected override void ProcessRecord() path); WriteError(errorRecord); } + catch (IOException ioException) + { + var errorRecord = new ErrorRecord( + ioException, + "FileReadError", + ErrorCategory.ReadError, + path); + WriteError(errorRecord); + } finally { openfilestream?.Dispose(); diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Get-FileHash.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Get-FileHash.Tests.ps1 index 283234005ba..3807d2d2ea0 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Get-FileHash.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Get-FileHash.Tests.ps1 @@ -28,6 +28,13 @@ Describe "Get-FileHash" -Tags "CI" { $result.Count | Should -Be 1 $errorVariable.FullyQualifiedErrorId | Should -BeExactly "UnauthorizedAccessError,Microsoft.PowerShell.Commands.GetFileHashCommand" } + + It "Should write non-terminating error if a file is locked" -Skip:(-not $IsWindows) { + $pagefilePath = (Get-CimInstance -ClassName Win32_PageFileusage).Name + $result = $pagefilePath, "${pshome}\pwsh.dll" | Get-FileHash -ErrorVariable errorVariable + $result.Count | Should -Be 1 + $errorVariable.FullyQualifiedErrorId | Should -BeExactly "FileReadError,Microsoft.PowerShell.Commands.GetFileHashCommand" + } } Context "Algorithm tests" {