有没有办法让 Read-Host 识别 KB、MB、GB、TB 和 PB 等 PowerShell 乘数?如果我手动填充 $freespace 变量,第一个示例可以正常工作,但在使用 Read-Host 填充 $freespace 变量的第二个示例中不起作用。必须是一个简单的解决方案,以提示用户输入并将值识别为整数而不是字符串。
#this works just fine. The PowerShell multiplier GB is recognized if typed in.
$freespace = 6GB
If ($freeSpace -le 5GB) {
Write-Host “Free disk space is less than 5 GB”
} ElseIf ($freeSpace -le 10GB) {
Write-Host “Free disk space is less than 10 GB”
} ElseIf ($freeSpace -le 20GB) {
Write-Host “Free disk space is less than 20 GB”
} Else {
Write-Host “Free disk space is more than 20 GB”
}
#This does not work. The variable is populated by Read-Host as a [string]
$freespace = Read-Host -Prompt 'Please enter a value for Freespace'
If ($freeSpace -le 5GB) {
Write-Host “Free disk space is less than 5 GB”
} ElseIf ($freeSpace -le 10GB) {
Write-Host “Free disk space is less than 10 GB”
} ElseIf ($freeSpace -le 20GB) {
Write-Host “Free disk space is less than 20 GB”
} Else {
Write-Host “Free disk space is more than 20 GB”
}