如何将变量调用为 属性 对象
How to call variable as property object
我目前正在修改脚本,我想像使用对象属性一样使用 $variable_expessions
:
Function View-Diskspace {
##formating output
$TotalGB = @{Name="Capacity(GB)";expression={[math]::round(($_.Capacity/ 1073741824),2)}}
$FreeGB = @{Name="FreeSpace(GB)";expression={[math]::round(($_.FreeSpace / 1073741824),2)}}
$FreePerc = @{Name="Free(%)";expression={[math]::round(((($_.FreeSpace / 1073741824)/($_.Capacity / 1073741824)) * 100),0)}}
Write-Host "`nGetting disk volume info from $machinename..." -ForegroundColor Cyan;
$volumes = Get-WmiObject -Class win32_volume -ComputerName localhost
#i want to detect $FreePerc with less than/or equal to 15 percent and volumes with no capacity, such as disc drives
if ($volumes | Select-Object Label, $FreePerc, $TotalGB | Where {$_.$FreePerc -le 15 -and $_.$TotalGB -ne 0})
{
Write-Host "`nVolume(s) about to reach full capacity:"
$volumes | Select-Object SystemName, DriveLetter, Label, $TotalGB, $FreeGB, $FreePerc | Where {$_.$FreePerc -le 15 -and $_.$TotalGB -ne 0} | Format-List
Write-Host "Please initiate drive volume clean up."
}
else
{
Write-Host "`n##> All volumes have more than 15% of free space"
}
}
TessellatingHeckler's comment is right。您正在正确定义计算属性并正确执行它们,但是您没有正确调用您的属性。
如果你 运行 $volumes | Select-Object Label, $FreePerc, $TotalGB | Get-Member
你会看到你应该使用的属性。这些在您计算的 属性 哈希表中定义:"Capacity(GB)"、"FreeSpace(GB)" 和 "Free(%)".
我简化了您的函数只是为了展示您尝试与之交互的属性的正确调用。
Function View-Diskspace {
# Calculated properties
$TotalGB = @{Name="Capacity(GB)";expression={[math]::round(($_.Capacity/ 1073741824),2)}}
$FreeGB = @{Name="FreeSpace(GB)";expression={[math]::round(($_.FreeSpace / 1073741824),2)}}
$FreePerc = @{Name="Free(%)";expression={[math]::round(((($_.FreeSpace / 1073741824)/($_.Capacity / 1073741824)) * 100),0)}}
# Get WMI information and use calculated properties
Get-WmiObject -Class win32_volume -ComputerName localhost |
Select-Object Label, $FreePerc, $TotalGB |
Where-Object {$_."Free(%)" -le 50 -and $_."Capacity(GB)" -ne 0} |
ForEach-Object -Begin {
Write-Host "`nVolume(s) about to reach full capacity:"
} -Process{
$_
}
}
仍然像以前一样使用 Select-Object
,但是在 where 子句中,我用您命名的字符串调用属性。
如果您真的想要让您的方式工作,您需要调用哈希表的名称属性。例如....
Where-Object {$_.($FreePerc.Name) -le 50}
我目前正在修改脚本,我想像使用对象属性一样使用 $variable_expessions
:
Function View-Diskspace {
##formating output
$TotalGB = @{Name="Capacity(GB)";expression={[math]::round(($_.Capacity/ 1073741824),2)}}
$FreeGB = @{Name="FreeSpace(GB)";expression={[math]::round(($_.FreeSpace / 1073741824),2)}}
$FreePerc = @{Name="Free(%)";expression={[math]::round(((($_.FreeSpace / 1073741824)/($_.Capacity / 1073741824)) * 100),0)}}
Write-Host "`nGetting disk volume info from $machinename..." -ForegroundColor Cyan;
$volumes = Get-WmiObject -Class win32_volume -ComputerName localhost
#i want to detect $FreePerc with less than/or equal to 15 percent and volumes with no capacity, such as disc drives
if ($volumes | Select-Object Label, $FreePerc, $TotalGB | Where {$_.$FreePerc -le 15 -and $_.$TotalGB -ne 0})
{
Write-Host "`nVolume(s) about to reach full capacity:"
$volumes | Select-Object SystemName, DriveLetter, Label, $TotalGB, $FreeGB, $FreePerc | Where {$_.$FreePerc -le 15 -and $_.$TotalGB -ne 0} | Format-List
Write-Host "Please initiate drive volume clean up."
}
else
{
Write-Host "`n##> All volumes have more than 15% of free space"
}
}
TessellatingHeckler's comment is right。您正在正确定义计算属性并正确执行它们,但是您没有正确调用您的属性。
如果你 运行 $volumes | Select-Object Label, $FreePerc, $TotalGB | Get-Member
你会看到你应该使用的属性。这些在您计算的 属性 哈希表中定义:"Capacity(GB)"、"FreeSpace(GB)" 和 "Free(%)".
我简化了您的函数只是为了展示您尝试与之交互的属性的正确调用。
Function View-Diskspace {
# Calculated properties
$TotalGB = @{Name="Capacity(GB)";expression={[math]::round(($_.Capacity/ 1073741824),2)}}
$FreeGB = @{Name="FreeSpace(GB)";expression={[math]::round(($_.FreeSpace / 1073741824),2)}}
$FreePerc = @{Name="Free(%)";expression={[math]::round(((($_.FreeSpace / 1073741824)/($_.Capacity / 1073741824)) * 100),0)}}
# Get WMI information and use calculated properties
Get-WmiObject -Class win32_volume -ComputerName localhost |
Select-Object Label, $FreePerc, $TotalGB |
Where-Object {$_."Free(%)" -le 50 -and $_."Capacity(GB)" -ne 0} |
ForEach-Object -Begin {
Write-Host "`nVolume(s) about to reach full capacity:"
} -Process{
$_
}
}
仍然像以前一样使用 Select-Object
,但是在 where 子句中,我用您命名的字符串调用属性。
如果您真的想要让您的方式工作,您需要调用哈希表的名称属性。例如....
Where-Object {$_.($FreePerc.Name) -le 50}