Powershell:如何使用数组或哈希表作为内联查找

Powershell: How can I use an array or hashtable as an inline lookup

我正在使用 Powershell win32_computersystem。我想列出有关机器的数据,包括 $_thermalstate - 这是我的代码

代码看起来应该可以工作,但 returns 是一个空值。我想创建一个内联数组或散列 table,值 $_.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)"]}}

请注意,查找 table 在 [] 的外部,并且该值被强制为字符串。


但是,我不会这样做。它太挑剔了。在您的呼叫之前创建查找 table 并使用它来执行查找。

您的代码看起来像是在尝试 declare/initialize 散列 table 同时还尝试将 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