【问题标题】:Powershell - Measure size of .tmp files (The property 'Length' cannot be found)Powershell - 测量 .tmp 文件的大小(找不到属性“长度”)
【发布时间】:2016-02-19 09:36:04
【问题描述】:

我正在尝试测量当前正在使用 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 文件的目录的递归大小。

【问题讨论】:

  • Get-ChildItem -Recurse 怎么样?它有没有输出任何文件?
  • 感谢您的提示!问题是文件被隐藏了,需要使用Get-ChildITem -Recurse -Hidden 开关显示。

标签: powershell temporary-files sccm microsoft-bits


【解决方案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 开关。

【讨论】:

    猜你喜欢
    • 2015-08-23
    • 2017-12-22
    • 2018-10-30
    • 2016-06-10
    • 1970-01-01
    • 2011-11-12
    • 1970-01-01
    • 2013-11-20
    • 1970-01-01
    相关资源
    最近更新 更多