Powershell - 测量.tmp文件的大小(找不到属性'Length')

时间:2016-02-19 09:36:04

标签: powershell temporary-files sccm microsoft-bits

我正在尝试测量当前使用BITS下载的ccmcache目录的递归大小。

我使用以下Powershell脚本来测量目录的递归大小。

(Get-ChildItem $downloadPath -recurse | Measure-Object -property Length -sum).Sum

此脚本适用于“普通”目录和文件,但如果目录仅包含.tmp个文件,则会失败,并显示以下错误。

Measure-Object : The property "Length" cannot be found in the input for any objects.
At line:1 char:27
+ (Get-ChildItem -Recurse | Measure-Object -Property Length -Sum).Sum
+                           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Measure-Object], PSArgumentException
    + FullyQualifiedErrorId : GenericMeasurePropertyNotFound,Microsoft.PowerShell.Commands.MeasureObjectCommand

如何衡量仅包含BITS下载程序创建的.tmp文件的目录的递归大小。

1 个答案:

答案 0 :(得分:1)

问题是BITS .tmp文件是隐藏的,Get-ChildItem默认只列出可见文件。

要测量整个目录的大小,包括隐藏文件,必须传递-Hidden开关。

(Get-ChildItem $downloadPath -Recurse -Hidden | Measure-Object -property Length -sum).Sum

但这只会计算隐藏文件,不包括所有可见文件。因此,为了计算所有文件,必须添加隐藏总和和可见总和的结果:

[long](Get-ChildItem $downloadPath -Recurse -Hidden | Measure-Object -property length -sum -ErrorAction SilentlyContinue).Sum + [long](Get-ChildItem $downloadPath -Recurse | Measure-Object -property length -sum -ErrorAction SilentlyContinue).Sum 

如果不存在隐藏文件或可见文件,则会发生错误。因此,包含-ErrorAction SilentlyContinue开关。