FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Write an error if argument is a directory in Get-FileHash cmdlet by iSazonov · Pull Request #11114 · PowerShell/PowerShell · GitHub

Write an error if argument is a directory in Get-FileHash cmdlet - #11114

Merged
Ilya (iSazonov) merged 5 commits into
PowerShell:masterfrom
iSazonov:get-filehash-dir-error
Nov 21, 2019
Merged

Write an error if argument is a directory in Get-FileHash cmdlet#11114
Ilya (iSazonov) merged 5 commits into
PowerShell:masterfrom
iSazonov:get-filehash-dir-error

Conversation

Ilya (iSazonov) commented Nov 19, 2019
edited
Loading

Copy link
Copy Markdown
Collaborator

PR Summary

Fix #11110

If Get-FileHash gets a folder as argument we should write non-terminate error.

PR Context

PR Checklist

Ilya (iSazonov) added the CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log label Nov 19, 2019

Copy link
Copy Markdown
Collaborator Author

Steve Lee (@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.

Ilya (iSazonov) self-assigned this Nov 19, 2019
Ilya (iSazonov) and others added 2 commits November 19, 2019 18:21
…Hash.cs

Co-Authored-By: Joel Sallow (/u/ta11ow) <32407840+vexx32@users.noreply.github.com>
…Hash.cs

Co-Authored-By: Joel Sallow (/u/ta11ow) <32407840+vexx32@users.noreply.github.com>
Comment on lines 141 to +158
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);
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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:

  • PathTooLongException
  • IOException

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

IOException combines FileNotFoundException, DirectoryNotFoundException, and PathTooLongException.

I guess PathTooLongException will be raised above at path resolving time.

So nothing to do. :-)

Copy link
Copy Markdown

🎉v7.0.0-rc.2 has been released which incorporates this pull request.:tada:

Handy links:

ghost mentioned this pull request Jan 16, 2020

Jordan M. Nash (JordanNash) commented Jun 8, 2021
edited
Loading

Copy link
Copy Markdown

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.
Error:
Get-FileHash: Access to the path 'E:\PHOTO\Lightroom CC Library\Lightroom CC\684f1da3ca494b3e854964d66fd6f341\originals\2011\2011-12-22' is denied.

Invocation:
Get-ChildItem -Recurse -path E:\PHOTO\ | Select-Object -ExpandProperty FullName | Get-FileHash | Export-Csv -Append h:\photo_hash_list3.csv

Copy link
Copy Markdown
Collaborator

You can add the -File param to your Get-ChildItem call to avoid the issue entirely.

But yeah, that's a fair call. Would be a good idea to open a new issue for that. 🙂

This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Get-FileHash access denied

6 participants


Back | FazBrowse Home | New Git URL