【问题标题】:Powershell to monitor disk usage - performance of script queryPowershell监控磁盘使用情况——脚本查询的性能
【发布时间】:2013-04-09 19:16:49
【问题描述】:

我正在开发以下 powershell 来报告我的磁盘使用情况,并让我了解哪些文件夹的大小正在发生变化等...

Get-ChildItem -Path "C:\" | 
Select-Object Name,
    @{ 
        Name="Size";
        Expression=
            { 
                [Math]::Round(
                    ((($_ | Get-ChildItem -Recurse | Measure-Object -Sum Length).Sum + 0) / 1GB)
                    , 2)
            }
    }

这个脚本真的很慢……为什么?

Measure-object 每次查看文件夹时都会评估大小吗?

有什么更好的方法来做到这一点?

谢谢

【问题讨论】:

    标签: powershell size disk


    【解决方案1】:

    您正在列出 C 驱动器上的每个文件系统对象并测量其大小,这可能需要很长时间才能完成。两种选择(还有更多):

    (Get-PSDrive c).Used/1gb
    
    - and -
    
    $cDrive = (New-Object -ComObject Scripting.FileSystemObject).GetDrive('C')
    $used = ($cDrive.TotalSize-$cDrive.FreeSpace)/1gb
    $used.ToString('N2')
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-05-18
      • 2013-05-03
      • 2011-08-04
      • 1970-01-01
      • 2021-02-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多