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

时间:2019-05-31 06:07:06

标签: arrays powershell hashtable

使用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

3 个答案:

答案 0 :(得分:3)

您的查找结构错误。 [咧嘴]

替换此重新格式化的代码版本的最后一行...

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上,并且该值被强制为字符串。


但是,我不会这样。太挑剔了。在调用之前创建查找表,并使用该表执行查找。

答案 1 :(得分:1)

您的代码看起来像您在尝试声明/初始化哈希表,同时还尝试将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()]}}

答案 2 :(得分:1)

根据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