Write an error if argument is a directory in Get-FileHash cmdlet#11114
Write an error if argument is a directory in Get-FileHash cmdlet#11114iSazonov merged 5 commits intoPowerShell:masterfrom
Conversation
|
@SteveL-MSFT If the new test uses $error[0]: It "Should write non-terminating error if argument is a folder" {
$result = $pshome, "${pshome}\pwsh.dll" | Get-FileHash
$result.Count | Should -Be 1
$error[0].FullyQualifiedErrorId | Should -BeExactly "UnauthorizedAccessError,Microsoft.PowerShell.Commands.GetFileHashCommand"
}it unexpectedly fail with an ConciseView error MethodInvocationException:
Line |
127 | $remainingMessage = $remainingMessage.Substring($substring.Length - $prefix.Length).Trim()
| ^ Exception calling "Substring" with "1" argument(s): "StartIndex cannot be less than zero. (Parameter 'startIndex')"I cannot find simple repo steps. |
src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetHash.cs
Outdated
Show resolved
Hide resolved
src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetHash.cs
Outdated
Show resolved
Hide resolved
…Hash.cs Co-Authored-By: Joel Sallow (/u/ta11ow) <[email protected]>
…Hash.cs Co-Authored-By: Joel Sallow (/u/ta11ow) <[email protected]>
| catch (FileNotFoundException ex) | ||
| { | ||
| ErrorRecord errorRecord = new ErrorRecord(ex, | ||
| var errorRecord = new ErrorRecord( | ||
| ex, | ||
| "FileNotFound", | ||
| ErrorCategory.ObjectNotFound, | ||
| path); | ||
| WriteError(errorRecord); | ||
| } | ||
| catch (UnauthorizedAccessException ex) | ||
| { | ||
| var errorRecord = new ErrorRecord( | ||
| ex, | ||
| "UnauthorizedAccessError", | ||
| ErrorCategory.InvalidData, | ||
| path); | ||
| WriteError(errorRecord); | ||
| } |
There was a problem hiding this comment.
Do you think it's worth doing a general catch or additional catches here as well? Looking at the exceptions that can be thrown from System.IO.File.OpenRead it seems likely we can run into the following exceptions somewhat commonly as well:
PathTooLongExceptionIOException
There was a problem hiding this comment.
IOException combines FileNotFoundException, DirectoryNotFoundException, and PathTooLongException.
I guess PathTooLongException will be raised above at path resolving time.
So nothing to do. :-)
|
🎉 Handy links: |
|
It would be appreciated if this error could be changed from "Access Denied" to something more specific, i.e.: "SKIPPED: Invalid path: folder" or something. I'm trying to recurse through my photo archive to try to find duplicates. For each file, I send FullName to get-filehash. I get a LOT of these errors, which implies a permissions issues, but after finding this pull request, I see that it is working as expected. Invocation: |
|
You can add the But yeah, that's a fair call. Would be a good idea to open a new issue for that. 🙂 |
PR Summary
Fix #11110
If Get-FileHash gets a folder as argument we should write non-terminate error.
PR Context
PR Checklist
.h,.cpp,.cs,.ps1and.psm1files have the correct copyright headerWIP:or[ WIP ]to the beginning of the title (theWIPbot will keep its status check atPendingwhile the prefix is present) and remove the prefix when the PR is ready.