Luc*_*kor 6 directory powershell get-childitem
使用PowerShell 4.0,我试图获得多个目录的大小,我得到的窗口告诉我和我的代码告诉我的结果非常不一致.
有问题的代码是:
$temp4 = ($folderInfo.rootFolder).fullname
$folderInfo.directories += Get-ChildItem -LiteralPath $temp4 -Recurse -Force -Directory
$folderInfo.directories += $folderInfo.rootFolder
foreach ($dir in $folderInfo.directories)
{
$temp3 = $dir.fullname
$temp2 = Get-ChildItem -LiteralPath $temp3 -Force
$temp = (Get-ChildItem -LiteralPath $dir.fullname -Force -File | Measure-Object -Property length -Sum -ErrorAction SilentlyContinue).Sum
$folderInfo.totalSize += $temp
}
return $folderInfo
Run Code Online (Sandbox Code Playgroud)
如果$folderInfo.rootFolder = D:\sample
那时我得到我想要的东西,但如果$folderInfo.rootFolder = D:\[sample
那时我得到了
Get-ChildItem:无法检索cmdlet的动态参数.指定的通配符模式无效:sample [sample at C:\ powershell scripts\test.ps1:55 char:12 + $ temp =(Get-ChildItem $ dir.fullname -Force -File | Measure-Object -Property l ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo:InvalidArgument :( :[Get-ChildItem],ParameterBindingException + FullyQualifiedErrorId:GetDynamicParametersException,Microsoft.PowerShell.Commands.GetChildItemCommand
如果D:\sample在它的子节点中包含某个文件夹,则同样适用"[sample".我将从其他所有内容中获得正确的结果,但是在问题目录中或之外的任何内容.双方$dir.pspath并$dir.fullname搞砸.
编辑:更改了上面的代码以反映它的当前状态并包含完整错误.
再次编辑:上面的代码现在有一些调试临时变量.
mjo*_*nor 15
使用-LiteralPath参数代替-Path来抑制通配符globbing.此外,由于您使用的是V4,因此您可以使用-Directory开关并省去过$_.iscontainer滤器:
$folderInfo.directories =
Get-ChildItem -LiteralPath $folderInfo.rootFolder -Recurse -Force -Directory
Run Code Online (Sandbox Code Playgroud)
如果在目录树的下方有更多squre括号,请在后续的Get-ChildItem命令中继续使用ligpath:
$folderInfo.directories += Get-ChildItem -LiteralPath $folderInfo.rootFolder -Recurse -Force -Directory
$folderInfo.directories += Get-Item -LiteralPath $folderInfo.rootFolder
foreach ($dir in $folderInfo.directories)
{
$temp2 = Get-ChildItem -LiteralPath $dir.PSPath -Force
$temp = (Get-ChildItem -LiteralPath $dir.fullname -Force -File | Measure-Object -Property length -Sum -ErrorAction SilentlyContinue).Sum
$folderInfo.totalSize += $temp
}
return $folderInfo
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3677 次 |
| 最近记录: |