我嘗試添加百分號(「%」)追加到一個變量結束:PowerShell的:附加一個百分號可變
$disk = Get-WmiObject -ComputerName $Computer -Class Win32_LogicalDisk -Filter "Caption = 'D:'"
If (!($disk)) {
$DiskpercentFree = "n/a"
}
Else {
$deviceID = $disk.DeviceID
[float]$size = $disk.Size;
[float]$freespace = $disk.FreeSpace;
$diskpercentFree1 = [Math]::Round(($freespace/$size) * 100)
$Percent = "%"
$diskpercentFree = $diskpercentFree1 + $Percent
}
但我得到的是:
Cannot convert value "%" to type "System.Double". Error: "Input string was not in a correct format."
推測是因爲它認爲「+」運算符正在計算?我已經嘗試了各種連續選項,但似乎無法做到正確。誰能幫忙?
我選擇了$ diskpercentFree = [String] $ diskpercentFree1 +「$ Percent」。我確實嘗試轉換變量:「[String] $ diskpercentFree」。非常感謝! – JDGEEK
@JDGEEK然後在技術上你應該接受其他答案,因爲那是列出該方法的那個 – arco444