从Get-Childitem捕获无法访问的文件夹的路径 [英] Catching paths of inaccessible folders from Get-Childitem

查看:77
本文介绍了从Get-Childitem捕获无法访问的文件夹的路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究小型脚本,以捕获正在运行的系统上的文件哈希.我只有Powershell可用.

I am working on small script to capture file hashes on a running system. I only have Powershell available.

这是代码的有效部分:

get-childitem -path $path -filter $filename -Recurse -Force | Select FullName | foreach-object { get-filehash $_.fullname | select * }

这是我正在测试的命令:

this is the command I am testing with:

./Get-FileHashesRecursive.ps1 -path c:\ -filename *.txt

运行脚本时,由于某些文件夹不可访问,我遇到了一系列错误.我想记录这些文件夹的路径,以便用户记录失败的完成情况.

When running the script I get a series of errors because certain folders are inaccessible. I'd like to record the paths of those folders so the user has a record on completion of what failed.

错误在控制台窗口中如下所示:

the error looks like this in a console window:

get-childitem : Access to the path 'C:\$Recycle.Bin\S-1-5-21-4167544967-4010527683-3770225279-9182' is denied.
At E:\git\Get-RemoteFileHashesRecursive\Get-FileHashesRecursive.ps1:14 char:9
+         get-childitem -path $path -filter $filename -Recurse -Force | ...
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : PermissionDenied: (C:\$Recycle.Bin...3770225279-9182:String) [Get-ChildItem], UnauthorizedAccessException
    + FullyQualifiedErrorId : DirUnauthorizedAccessError,Microsoft.PowerShell.Commands.GetChildItemCommand

有没有一种方法可以抓住错误的路径或整个第一行而又不停止脚本的其余部分运行?

Is there a way I can grab the path or the entire first line of the error WITHOUT stopping the rest of the script from running?

推荐答案

根据要求,以下是我先前的评论作为答案:

As requested, here's my earlier comments as an answer:

Get-ChildItem -Path $Path -Filter $Filename -File -Recurse -Force -ErrorVariable FailedItems -ErrorAction SilentlyContinue | ForEach-Object { Get-FileHash -Path $_.FullName | Select-Object * }
$FailedItems | Foreach-Object {$_.CategoryInfo.TargetName} | Out-File "C:\Users\sailingbikeruk\Desktop\noaccess.log"

  • 我已将 -File 参数添加到 Get-ChildItem ,因为您专门处理的只是文件.
  • 我还向 Get-ChildItem 命令中添加了 -ErrorVariable -ErrorAction 参数. -ErrorVariable FailedItems 定义了一个变量的自定义名称,该变量在处理过程中存储了命令中的错误. -ErrorAction SilentlyContinue ,告诉脚本继续运行而不通知您错误.
  • 命令完成处理后,就可以解析 $ FailedItems 变量的内容.在上面的示例中,我已经将 TargetName 输出到文件中,以便您可以随时阅读(请记住,根据需要调整其文件路径和名称,如果您还希望将其输出到文件中.)
    • I have added the -File parameter to Get-ChildItem, because you are specifically dealing with only files.
    • I also added the -ErrorVariable and -ErrorAction parameters to the Get-ChildItem command. -ErrorVariable FailedItems defines a custom name for a variable which stores errors from the command during processing. -ErrorAction SilentlyContinue, tells the script to continue without notifying you of the errors.
    • Once your command has finished processing, you can parse the content of the $FailedItems variable. In the example above, I've output the TargetName to a file so that you can read it at your leisure, (please remember to adjust its file path and name as needed, should you also wish to output it to a file).
    • 这篇关于从Get-Childitem捕获无法访问的文件夹的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆