[英]PowerShell - Get-ChildItem does not exclude directory
当递归搜索特定目录时, 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目录。
我正在编辑此脚本,该脚本在多个服务器上运行。 我正在笔记本电脑上对其进行测试。 我仍然不明白,一旦找不到起始路径,为什么会遍历文件系统的其余部分。
看起来(我还没有进行足够的搜索),这可能是Get-ChildItem
的错误; 如果将不存在的路径传递给-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";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.