[英]Powershell: How can I use an array or hashtable as an inline lookup
使用Powershell我正在調用win32_computersystem
。 我想列出有關該機器的數據,包括$_thermalstate
- 這是我的代碼
代碼看起來好像應該工作但返回一個空值。 我想創建一個值$_.thermalstate
引用的內聯數組或哈希表。
Get-WmiObject win32_computersystem | select Name, Model, Caption, @{n="Timezone"; e={$_.currenttimezone}}, Description, DNShostname,Domain,@{n='Domain Role'; E={$_.domainrole}},Roles,Status,@{n='System Type'; e={$_.systemtype}},@{n='Thermal State'; e={$_.thermalstate[@{'3'='safe'}]}}
產量
Name : MYPC
Model : Latitude E5470
Caption : MYPC
Timezone : 600
Description : AT/AT COMPATIBLE
DNShostname : MYPC
Domain : work.biz
Domain Role : 1
Roles : {LM_Workstation, LM_Server, NT}
Status : OK
System Type : x64-based PC
Thermal State : Safe
你的查找結構是......錯了。 [ 笑 ]
替換此重新格式化代碼版本的最后一行...
Get-WmiObject win32_computersystem |
Select-Object Name, Model, Caption,
@{n="Timezone"; e={$_.currenttimezone}},
Description, DNShostname,Domain,
@{n='Domain Role'; E={$_.domainrole}},
Roles,Status,
@{n='System Type'; e={$_.systemtype}},
@{n='Thermal State'; e={$_.thermalstate[@{'3'='safe'}]}}
......用這條線......
@{n='Thermal State'; e={@{'3'='Safe'}["$($_.ThermalState)"]}}
請注意,查找表位於[]
的OUTSIDE上,並且該值被強制為字符串。
但是,我不會這樣做。 它太挑剔了。 在調用之前創建查找表並使用它來執行查找。
您的代碼看起來像是在嘗試聲明/初始化哈希表,同時還嘗試使用thermalstate作為哈希數組。 如果首先初始化哈希數組,代碼如下所示:
$h = @{'3'='safe'}; Get-WmiObject win32_computersystem | select Name, Model, Caption, @{n="Timezone"; e={$_.currenttimezone}}, Description, DNShostname,Domain,@{n='Domain Role';E={$_.domainrole}},Roles,Status,@{n='System Type'; e={$_.systemtype}},@{n='Thermal State'; e={$h[$_.thermalstate.toString()]}}
根據https://wutils.com/wmi/root/cimv2/win32_computersystem/
ThermalState property
CIMTYPE 'uint16'
Description 'The ThermalState property identifies the enclosure's thermal state when last booted.'
MappingStrings ['SMBIOS|Type 3|System Enclosure or Chassis|Thermal State']
read True
ValueMap ['1', '2', '3', '4', '5', '6']
Values ['Other', 'Unknown', 'Safe', 'Warning', 'Critical', 'Non-recoverable']
ThermalState property is in 1 class (Win32_ComputerSystem) of ROOT\cimv2 and in 2 namespaces
你可以創建一個枚舉
enum ThermalState {
Other = 1
Unknown = 2
Safe = 3
Warning = 4
Critical = 5
NonRecoverable = 6
}
並使用它來獲得該物業的詳細回應
Get-WmiObject win32_computersystem | Select-Object Name, Model, Caption,
@{n="Timezone"; e={$_.currenttimezone}}, Description, DNShostname,Domain,
@{n='Domain Role';E={$_.domainrole}},Roles,Status,
@{n='System Type'; e={$_.systemtype}},
@{n='Thermal State'; e={[ThermalState]$_.thermalstate}}
樣本輸出
Name : HP-G1610
Model : ProLiant MicroServer Gen8
Caption : HP-G1610
Timezone : 120
Description : AT/AT COMPATIBLE
DNShostname : HP-G1610
Domain : DOMAIN
Domain Role : 0
Roles : {...}
Status : OK
System Type : x64-based PC
Thermal State : Safe
一般來說,獲取枚舉列表:
> $Enum ='System.DayOfWeek'
> [Enum]::GetValues($Enum) | ForEach-Object {'{0} {1}' -f [int]$_,$_ }
0 Sunday
1 Monday
2 Tuesday
3 Wednesday
4 Thursday
5 Friday
6 Saturday
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.