mirror of
https://github.com/PowerShell/PowerShell
synced 2026-06-08 12:12:50 +00:00
Don't write an error if file already unblocked (Unblock-File) (#5362)
This commit is contained in:
@@ -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));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -174,4 +174,7 @@
|
||||
<data name="TypeNotSupported" xml:space="preserve">
|
||||
<value>'{0}' is not supported in this system.</value>
|
||||
</data>
|
||||
<data name="NoZoneIdentifierFileStream" xml:space="preserve">
|
||||
<value>The file is not blocked: {0}</value>
|
||||
</data>
|
||||
</root>
|
||||
|
||||
@@ -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"
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user