diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/UnblockFile.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/UnblockFile.cs index 05305d819b..5578654c42 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/UnblockFile.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/UnblockFile.cs @@ -121,9 +121,17 @@ namespace Microsoft.PowerShell.Commands } catch (Win32Exception accessException) { - WriteError(new ErrorRecord(accessException, "RemoveItemUnauthorizedAccessError", ErrorCategory.PermissionDenied, path)); + // NativeErrorCode=2 - File not found. + // If the block stream not found the 'path' was not blocked and we successfully return. + if (accessException.NativeErrorCode != 2) + { + WriteError(new ErrorRecord(accessException, "RemoveItemUnauthorizedAccessError", ErrorCategory.PermissionDenied, path)); + } + else + { + WriteVerbose(StringUtil.Format(UtilityCommonStrings.NoZoneIdentifierFileStream, path)); + } } - } } } diff --git a/src/Microsoft.PowerShell.Commands.Utility/resources/UtilityCommonStrings.resx b/src/Microsoft.PowerShell.Commands.Utility/resources/UtilityCommonStrings.resx index f8e5623d8f..651d75d1a3 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/resources/UtilityCommonStrings.resx +++ b/src/Microsoft.PowerShell.Commands.Utility/resources/UtilityCommonStrings.resx @@ -174,4 +174,7 @@ '{0}' is not supported in this system. + + The file is not blocked: {0} + diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Unblock-File.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Unblock-File.Tests.ps1 index 4965571b9a..11bf4e4cec 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Unblock-File.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Unblock-File.Tests.ps1 @@ -47,6 +47,9 @@ Describe "Unblock-File" -Tags "CI" { It "With '-Path': file exist" { Unblock-File -Path $testfilepath Test-UnblockFile | Should Be $true + + # If a file is not blocked we silently return without an error. + { Unblock-File -Path $testfilepath -ErrorAction Stop } | Should Not Throw } It "With '-LiteralPath': file exist" { @@ -67,7 +70,6 @@ Describe "Unblock-File" -Tags "CI" { $TestFileCreated = Get-ChildItem $TestFile $TestFileCreated.IsReadOnly | Should Be $true - { Unblock-File -LiteralPath $TestFile -ErrorAction SilentlyContinue } | Should Not Throw - $error[0].FullyQualifiedErrorId | Should Be "RemoveItemUnauthorizedAccessError,Microsoft.PowerShell.Commands.UnblockFileCommand" + { Unblock-File -LiteralPath $TestFile -ErrorAction Stop } | ShouldBeErrorId "RemoveItemUnauthorizedAccessError,Microsoft.PowerShell.Commands.UnblockFileCommand" } }