我如何从 cmdlet 命令获取属性并将它们作为整数保存到变量
How do i get properties from cmdlet commands and save them as a integers to a variable
例如,我试图从 cmdlet 获取一个值,将其保存为 int 并在 PowerShell 脚本的 if 语句或 while 语句中使用它。
$value = Get-WmiObject -Class Win32_logicaldisk -过滤器"DriveType = '2'" | Select-对象大小
回声$值
Size
----
1992032256 <------- I am trying to get that number so that I can use it in a if statement and compare it with another number
像这样:
$value = Get-WmiObject -Class Win32_logicaldisk -Filter "DriveType = '2'"
$value.size
Get-WmiObject -Class Win32_logicaldisk -Filter "DriveType = '2'"
语句可能 return 不止一个对象。
要将 Size 属性与某个值进行比较,您需要迭代结果:
Get-WmiObject -Class Win32_logicaldisk -Filter "DriveType = '2'" | ForEach-Object {
# for demo, output to console if the Size is greater or equal then 16 GB
$sizeGB = [math]::Round($_.Size / 1GB) # Size is an Int64 value in Bytes
if ($sizeGB -ge 16) {
Write-Host "Drive $($_.DeviceID) has a total capacity of $sizeGB GB"
}
}
例如,我试图从 cmdlet 获取一个值,将其保存为 int 并在 PowerShell 脚本的 if 语句或 while 语句中使用它。
$value = Get-WmiObject -Class Win32_logicaldisk -过滤器"DriveType = '2'" | Select-对象大小 回声$值
Size
----
1992032256 <------- I am trying to get that number so that I can use it in a if statement and compare it with another number
像这样:
$value = Get-WmiObject -Class Win32_logicaldisk -Filter "DriveType = '2'"
$value.size
Get-WmiObject -Class Win32_logicaldisk -Filter "DriveType = '2'"
语句可能 return 不止一个对象。
要将 Size 属性与某个值进行比较,您需要迭代结果:
Get-WmiObject -Class Win32_logicaldisk -Filter "DriveType = '2'" | ForEach-Object {
# for demo, output to console if the Size is greater or equal then 16 GB
$sizeGB = [math]::Round($_.Size / 1GB) # Size is an Int64 value in Bytes
if ($sizeGB -ge 16) {
Write-Host "Drive $($_.DeviceID) has a total capacity of $sizeGB GB"
}
}