计算变量时出错

Getting Error while calculation on variable

我想计算特定文件夹的使用百分比 space 并显示与阈值(即 85%)相比的值,但是当我编写以下代码时,出现错误:

Cannot convert the "Microsoft.PowerShell.Commands.GenericMeasureInfo" value of type "Microsoft.PowerShell.Commands.GenericMeasureInfo" to type "System.Double".

代码:

$Path = "C:\Nikhil"
$Workspace = (Get-ChildItem $Path -recurse | Measure-Object -property length -sum)
"{0:N2}" -f ($Workspace.sum / 1GB)
$Workspace1 = $Workspace -as [double]
$Percentage = ($Workspace1/6143.87)*100
if($Percentage -le 85)
{
    $body += "`t" + $Path + " is $Percentage percent utilized. "
}

你能指正我哪里做错了吗?提前致谢。

这一行

$Workspace1 = $Workspace -as [double]

应该是

$Workspace1 = $Workspace.Sum -as [double]

因为$Workspace是一个有属性的测量对象,不是一个数字。