尝试根据百分比实现 if 语句
Trying to implement if statement based on percentage
我正在构建一个脚本,但我无法完成它,因为我正在努力让 if
语句正常工作。
$freespace = [math]::round((Get-WmiObject Win32_Volume -Filter "Label='User Disk'" | Foreach-Object {$_.FreeSpace})/ 1MB)
$volumespace = [math]::round((Get-WmiObject Win32_Volume -Filter "Label='User Disk'" | Foreach-Object {$_.Capacity})/ 1MB)
$usedspace=$volumespace-$freespace
Write-Host
"Used Space: $usedspace"
"Free Space: $freespace"
"Total Space Assigned: $volumespace"
if ($freespace -lt 5% $volumespace)
if
语句需要这样计算(伪代码):
if $freespace is less than 5% of the $volumespace
然后我的发送邮件命令附加到它。
尽管苦苦挣扎了几个小时,我还是不知道如何进行这个计算。
$freeSpace -lt $volumeSpace * .05
是您要查找的条件:
-lt
是 PowerShell 小于运算符(<
在其他语言中)
- 小数
.5
(0.05
)代表5%,类型[double]
,和你的变量一样
您的代码的以下简化版本演示了如何使用 变量 来存储百分比:
$freeSpace, $volumeSpace = Get-WmiObject Win32_Volume -Filter "Label='User Disk'" |
ForEach-Object { [math]::Round($_.FreeSpace / 1mb), [math]::Round($_.Capacity / 1mb) }
$usedSpace = $volumeSpace - $freeSpace
$thresholdPercentage = 5
# Send email if there's less than 5% free space.
# Note the need to divide the percentage number by 100.
# Alternatively, define $thresholdPercentage as 0.05 above.
if ($freeSpace -lt $volumeSpace * ($thresholdPercentage / 100)) {
# Send email
}
我正在构建一个脚本,但我无法完成它,因为我正在努力让 if
语句正常工作。
$freespace = [math]::round((Get-WmiObject Win32_Volume -Filter "Label='User Disk'" | Foreach-Object {$_.FreeSpace})/ 1MB)
$volumespace = [math]::round((Get-WmiObject Win32_Volume -Filter "Label='User Disk'" | Foreach-Object {$_.Capacity})/ 1MB)
$usedspace=$volumespace-$freespace
Write-Host
"Used Space: $usedspace"
"Free Space: $freespace"
"Total Space Assigned: $volumespace"
if ($freespace -lt 5% $volumespace)
if
语句需要这样计算(伪代码):
if $freespace is less than 5% of the $volumespace
然后我的发送邮件命令附加到它。
尽管苦苦挣扎了几个小时,我还是不知道如何进行这个计算。
$freeSpace -lt $volumeSpace * .05
是您要查找的条件:
-lt
是 PowerShell 小于运算符(<
在其他语言中)- 小数
.5
(0.05
)代表5%,类型[double]
,和你的变量一样
您的代码的以下简化版本演示了如何使用 变量 来存储百分比:
$freeSpace, $volumeSpace = Get-WmiObject Win32_Volume -Filter "Label='User Disk'" |
ForEach-Object { [math]::Round($_.FreeSpace / 1mb), [math]::Round($_.Capacity / 1mb) }
$usedSpace = $volumeSpace - $freeSpace
$thresholdPercentage = 5
# Send email if there's less than 5% free space.
# Note the need to divide the percentage number by 100.
# Alternatively, define $thresholdPercentage as 0.05 above.
if ($freeSpace -lt $volumeSpace * ($thresholdPercentage / 100)) {
# Send email
}