当递归地通过特定目录进行搜索时,Get-ChildItem将从根目录开始搜索。我不想在C:\Windows目录中搜索。我想将搜索限制在C:\Docs目录下。
下面是我要做的事:
PS> Get-ChildItem -path “C:\docs” -Filter "*crypt*" -recurse -ErrorAction Stop
Get-ChildItem : Access to the path 'C:\Windows\CSC' is denied.
At line:1 char:1
+ Get-ChildItem -path “C:\docs” -Filter "*crypt*" -recurse -ErrorAction ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : PermissionDenied: (C:\Windows\CSC:String) [Get-ChildItem], UnauthorizedAccessException
+ FullyQualifiedErrorId : DirUnauthorizedAccessError,Microsoft.PowerShell.Commands.GetChildItemCommand
PS> Get-ChildItem -path “C:\docs” -Filter "*crypt*" -exclude "C:\windows" -recurse -ErrorAction Stop
Get-ChildItem : Access to the path 'C:\Windows\CSC' is denied.
At line:1 char:1
+ Get-ChildItem -path “C:\docs” -Filter "*crypt*" -exclude "C:\windows" ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : PermissionDenied: (C:\Windows\CSC:String) [Get-ChildItem], UnauthorizedAccessException
+ FullyQualifiedErrorId : DirUnauthorizedAccessError,Microsoft.PowerShell.Commands.GetChildItemCommand 编辑: TL;DR:用户错误。我没有C:\Docs目录。
我正在编辑这个脚本,它运行在几个服务器上。我正在我的笔记本上测试。我仍然不明白为什么当它找不到起始路径时,它会查看文件系统的其余部分。
发布于 2015-01-29 19:19:27
看起来(我搜索得还不够),这可能是Get-ChildItem中的一个bug;如果您将一个不存在的路径传递给-path参数,它将从该驱动器的根(至少是本地驱动器)进行搜索。
在调用Get-ChildItem之前,测试路径的存在性,这样就可以避免这种情况了。
$mypath = "c:\docs";
if (test-path -path $mypath) {
Get-childitem –path $mypath –filter “*crypt*" -recurse -ErrorAction stop;
} else {
Write-Warning "$mypath not found";
}https://stackoverflow.com/questions/28221935
复制相似问题