尝试扫描Powershell中的目标目录时出错。
我正在使用的代码是;
$path = read-host 'Enter the target drive letter'
$objects = get-childitem $path -force -recurse
基本上我想要做的是让这个常规搜索工作(扫描用户指定的位置),然后我可以优化它以按文件类型,大小等搜索特定项目,例如文件。当路径包含空格时出现错误,例如驱动器中的“文档和设置”或“程序文件”文件夹。
有没有办法可以在不收到此错误的情况下执行此操作?我对powershell很陌生,我在其他任何地方都看不到这个问题,但如果其他地方已经有了这个问题,我很抱歉。
它在Powershell v2.0上运行。考虑到这一点,我认为它与空间有关的原因是因为这些是唯一看似错误的项目,但是信息本身就说明了;
Get-ChildItem : Access to the path 'C:\Documents and Settings' is denied. At
C:\users\robert\desktop\v1.ps1:5 char:25 + $objects = get-childitem <<<< $path
- force -recurse + CategoryInfo : PermissionDenied: (C:\Documents and
Settings:String) [Get-ChildItem], UnauthorizedAccessException +
FullyQualifiedErrorId :
DirUnauthorizedAccessError,Microsoft.PowerShell.Commands.GetChildItemCommand
很抱歉,如果这是误导性的,我认为我作为管理员以无限制的执行策略运行此权限应该不是问题,但如果这实际上是导致它有一种方法来覆盖它所以它将扫描目标驱动器上的所有内容?
答案 0 :(得分:0)
使用:(请参阅其他双引号)
$objects = get-childitem "$path" -force -recurse
答案 1 :(得分:0)
Vista还是Windows 7?也许没问题,如果它只是在根目录上的“文档和设置”junction上绊倒了。交汇点链接到c:\ users,并且gci不会跟随链接,以防止循环或不完整的搜索。这是另一种偶然发现的方法:
at vanilla cmd:
cd 'C:\documents and settings'
explorer .
在Win7上,我收到一条弹出错误消息:
Location is not available
C:\documents and settings is not accessible
Access is denied
cd 'C:\documents and settings'
没有错误,shell返回该文件夹内的提示符(而不是链接目标,c:\ users);并运行单个命令explorer 'C:\documents and settings'
启动我的文档文件夹没有错误...但我离题。
我们不希望我们的递归搜索在无休止的loops中运行,也不希望在“文档和设置”和用户之间跳过文件夹。也许get-childItem应该以更好的方式处理这个问题,dunno。
同时,您可以通过这种方式排除交接点。我没有以这种方式测试大型递归搜索,但它们可能会运行更长时间。
gci "c:\" -force | where {($_.attributes.toString() -like "*ReparsePoint*") -eq $false}
除非我们将$true
换成$false
,否则结果不会包含“文档和设置”。