From 282b600056f5044770bdf37f48cfdca7d58602af Mon Sep 17 00:00:00 2001 From: iSazonov Date: Tue, 7 Nov 2017 10:18:24 +0300 Subject: [PATCH 1/5] Don't write an error if file already unblocked --- .../commands/utility/UnblockFile.cs | 7 ++++++- .../Microsoft.PowerShell.Utility/Unblock-File.Tests.ps1 | 3 +++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/UnblockFile.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/UnblockFile.cs index 05305d819bb..7bd6ec7d4e5 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/UnblockFile.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/UnblockFile.cs @@ -121,7 +121,12 @@ protected override void ProcessRecord() } catch (Win32Exception accessException) { - WriteError(new ErrorRecord(accessException, "RemoveItemUnauthorizedAccessError", ErrorCategory.PermissionDenied, path)); + // -2147467259 - File not found. + // If the block stream not found the 'path' file was not blocked and we successfully return. + if (accessException.HResult != -2147467259) + { + WriteError(new ErrorRecord(accessException, "RemoveItemUnauthorizedAccessError", ErrorCategory.PermissionDenied, path)); + } } } 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 4965571b9ad..85a49eefc46 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" { From 668028bcc5ef3f34da13a2cf3fd9d47306ac980d Mon Sep 17 00:00:00 2001 From: iSazonov Date: Tue, 7 Nov 2017 10:45:00 +0300 Subject: [PATCH 2/5] Fix test --- .../commands/utility/UnblockFile.cs | 6 +++--- .../Microsoft.PowerShell.Utility/Unblock-File.Tests.ps1 | 3 +-- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/UnblockFile.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/UnblockFile.cs index 7bd6ec7d4e5..2979c0e2482 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/UnblockFile.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/UnblockFile.cs @@ -121,9 +121,9 @@ protected override void ProcessRecord() } catch (Win32Exception accessException) { - // -2147467259 - File not found. - // If the block stream not found the 'path' file was not blocked and we successfully return. - if (accessException.HResult != -2147467259) + // 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)); } 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 85a49eefc46..11bf4e4cecd 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Unblock-File.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Unblock-File.Tests.ps1 @@ -70,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" } } From bf61faa9a90e11d542511eef2dbda4e4e9548bde Mon Sep 17 00:00:00 2001 From: iSazonov Date: Wed, 8 Nov 2017 09:05:07 +0300 Subject: [PATCH 3/5] Add verbose message --- .../commands/utility/UnblockFile.cs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/UnblockFile.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/UnblockFile.cs index 2979c0e2482..2cc8c3ce782 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/UnblockFile.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/UnblockFile.cs @@ -127,6 +127,10 @@ protected override void ProcessRecord() { WriteError(new ErrorRecord(accessException, "RemoveItemUnauthorizedAccessError", ErrorCategory.PermissionDenied, path)); } + else + { + WriteVerbose("File stream 'Zone.Identifier': "+accessException.Message); + } } } From 19fa0eda65c73104a52eaf3f039c76bc2b370337 Mon Sep 17 00:00:00 2001 From: iSazonov Date: Thu, 9 Nov 2017 10:28:51 +0300 Subject: [PATCH 4/5] Add a resource string --- .../commands/utility/UnblockFile.cs | 3 +-- .../resources/UtilityCommonStrings.resx | 3 +++ 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/UnblockFile.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/UnblockFile.cs index 2cc8c3ce782..5578654c42c 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/UnblockFile.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/UnblockFile.cs @@ -129,10 +129,9 @@ protected override void ProcessRecord() } else { - WriteVerbose("File stream 'Zone.Identifier': "+accessException.Message); + 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 f8e5623d8fc..7ed33339045 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. + + "No 'Zone.Identifier' file stream. The file was already not blocked: {0}" + From 81fbd1a3f15a7f844b893f53a56334527a2c039e Mon Sep 17 00:00:00 2001 From: iSazonov Date: Fri, 10 Nov 2017 06:43:31 +0300 Subject: [PATCH 5/5] Change a resource string --- .../resources/UtilityCommonStrings.resx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/resources/UtilityCommonStrings.resx b/src/Microsoft.PowerShell.Commands.Utility/resources/UtilityCommonStrings.resx index 7ed33339045..651d75d1a32 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/resources/UtilityCommonStrings.resx +++ b/src/Microsoft.PowerShell.Commands.Utility/resources/UtilityCommonStrings.resx @@ -175,6 +175,6 @@ '{0}' is not supported in this system. - "No 'Zone.Identifier' file stream. The file was already not blocked: {0}" + The file is not blocked: {0}