【发布时间】:2019-01-19 01:10:30
【问题描述】:
我有一个遍历目录树的简单脚本。
function GetSubFolders([IO.DirectoryInfo]$folder) {
Write-Host "Getting folder $folder"
Get-ChildItem $folder | ? { $_.PSIsContainer } | % { GetSubFolders $_ }
}
如果我调用GetSubFolders "c:\temp",它对于在 c:\temp: 中找到的每个子目录都会失败
Get-ChildItem : 找不到路径 'C:\WINDOWS\system32\somefolder
其中“somefolder”是 c:\temp 的子目录。变量$_ 明明是System.IO.DirectoryInfo 对象,但是为什么它被剥离了绝对路径,突然相对于当前目录c:\windows\system32?请注意,进入此方法后,它会正确输出
获取文件夹 c:\temp
这意味着它只工作一次,所以 Get-ChildItem 接受 DirectoryInfo 对象作为参数不会有问题。
【问题讨论】:
-
您尝试使用仅匹配
Get-ChildItem.的-Name属性的“按名称”或“按位置”的对象 [grin] 看看此[io.directoryinfo]'C:\temp' | Select-Object -Property *,然后将其与此(Get-Help Get-ChildItem -Parameter *).Name中的列表进行比较。请注意第一个对象中缺少任何-Path或 -LiteralPath` 属性名称。 -
为什么它只能工作一次,这就是我感到困惑的原因。
-
我不知道……对我来说似乎总是失败。
标签: powershell recursion get-childitem