Search code examples
arrayspowershellhashtable

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


Using Powershell I am calling win32_computersystem. I want to list data about the machine including $_thermalstate - Here is my code

The code looks as though it should work but returns a empty value. I want to create an inline array or hash table that the value $_.thermalstate references.

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'}]}}

Output

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

Solution

  • Your code looks like you are trying to declare/initialize a hash table while also trying to use thermalstate as a hash array. If you initialize the hash array first, the code looks like this:

    $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()]}}