使用PowerShell 4.0,我试图获取多个目录的大小,并且在windows告诉我的内容和代码告诉我的内容之间得到非常不一致的结果。
有关守则是:
$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如果$folderInfo.rootFolder = D:\sample,我得到了我想要的,但是如果$folderInfo.rootFolder = D:\[sample,我得到了
无法检索cmdlet的动态参数。指定的通配符字符模式无效: sample [示例在C:\powershell scripts\test.ps1:55 char:12 + $temp =(Get-char $dir.fullname -Force -File -Property -Object-Property l.+ CategoryInfo : InvalidArgument:(:) Get-ChildItem,ParameterBindingException + FullyQualifiedErrorId : GetDynamicParametersException,CategoryInfo)
如果D:\sample在其子目录中包含一个文件夹,即"[sample",情况也是如此。我将从所有其他方面得到正确的结果,但问题目录中或问题目录之外的任何内容都是如此。$dir.pspath和$dir.fullname都把事情搞砸了。
编辑:更改上述代码以反映其当前状态,并包含完整的错误。
再次编辑:上面的代码现在有一些调试临时变量。
发布于 2014-02-06 21:22:00
使用-LiteralPath参数代替-Path来抑制通配符全局化。此外,由于您使用的是V4,所以可以使用-Directory开关并免除$_.iscontainer筛选器:
$folderInfo.directories =
Get-ChildItem -LiteralPath $folderInfo.rootFolder -Recurse -Force -Directory 如果目录树下有更多squre括号,请在后续的Get-ChildItem命令中继续使用文字路径:
$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 $folderInfohttps://stackoverflow.com/questions/21613997
复制相似问题