如果驱动器的可用空间是100%空闲并且我将最小阈值空间设置为25%,则脚本错误地报告驱动器低于阈值...意味着100< 25 虽然如果我要更换 $ FreePercent =“{0:N0}” - f(100 * $ objDisk.FreeSpace / $ objDisk.Size)with $ FreePercent =(100 * $ objDisk.FreeSpace / $ objDisk.Size)... 我得到了正确的结果。 我如何解决四舍五入问题。 这是我的代码
# For Loop - get % free space of all drives
#Define Variables
$Notify = 0
$MinFreePercent = "25"
#$FileDriveSpace = "c:\temp\DriveSpace.txt"
$ComputerName = $(Get-WmiObject Win32_Computersystem).name
$OutArray = @()
$outarray += "Disk space Alerts and Utilizations on server $ComputerName"
$AllDisks = get-wmiobject Win32_LogicalDisk -Filter “DriveType = 3"
#Omit the a, b drives if exist
$AllDisks = $AllDisks | ? { $_.DeviceID -notmatch "[ab]:"}
foreach ($objdisk in $AllDisks)
{
#$FreePercent = "{0:P0} " -f ($objDisk.FreeSpace/$objDisk.Size)
$FreePercent = "{0:N0}" -f (100 * $objDisk.FreeSpace/$objDisk.Size)
If ($FreePercent -lt $MinFreePercent)
{
$Threshold = "Threshold Reached"
$Notify = 1
}
Else
{ $Threshold = "N/A"}
$DeviceId = $objDisk.DeviceID
$myobj = "" | Select "Drive","PercentFreeSpace", "Threshold"
$myobj.Drive = $DeviceId
$myobj.PercentFreeSpace = $FreePercent
$myobj.Threshold = $Threshold
$outLine = $DeviceId + " " + $FreePercent + " " + $Threshold + " " + $MinFreePercent
$outLine
#Add the object to the out-array
$outarray += $myobj
#Wipe the object just to be sure
$myobj = $null
}
答案 0 :(得分:0)
所以这里的问题是-f
或格式化运算符。它将你的超级驱动器百分比免费[int]
并将其变成一个普通的无聊[string]
。我个人使用[math]
加速器并调用圆方法。
[math]::Round($mynumber,0)
您还可以通过在technet库中查找此类事物来节省一些时间: http://gallery.technet.microsoft.com/Get-HardDrive-0eef638f
我不记得该版本是否将数据作为字符串或整数返回,但您会看到一个圆形方法的示例。