我在“ Windows PowerShell Tip of the Week ”中提供了一个稍微修改过的脚本版本。这个想法是确定文件夹及其子文件夹的大小:
$startFolder = "C:\Temp\"
$colItems = (Get-ChildItem $startFolder | Measure-Object | Select-Object -ExpandProperty count)
"$startFolder -- " + "{0:N2}" -f ($colItems.sum / 1MB) + " MB"
$colItems = (Get-ChildItem $startFolder -recurse | Where-Object {$_.PSIsContainer -eq $True} | Sort-Object)
foreach ($i in $colItems)
{
$subFolderItems = (Get-ChildItem $i.FullName | Measure-Object -property length -sum)
$i.FullName + " -- " + "{0:N2}" -f ($subFolderItems.sum / 1MB) + " MB"
}
该脚本运行良好,但对于某些文件夹,我收到一条错误消息:
Measure-Object : Property "length" cannot be found in any object(s) input.
At line:10 char:70
+ $subFolderItems = (Get-ChildItem $i.FullName | Measure-Object <<<< -property length -sum)
+ CategoryInfo : InvalidArgument: (:) [Measure-Object], PSArgumentException
+ FullyQualifiedErrorId : GenericMeasurePropertyNotFound,Microsoft.PowerShell.Commands.MeasureObjectCommand
此错误的原因是什么以及如何改进脚本以克服?