I am trying to pass a hashtable of calculated properties in to a query, for use with Select-Object
. It works when run in the console. I can confirm that the job is reading the hashtable as it lists the selected properties in the result, yet all of their values are null.
Note: I understand that I do not need to type cast these properties. I just demonstrating my issue.
If I run the following code (it looks weird, but there's actually a use case for this) the output contains my selected properties (from $globalConfig.SystemState.Processors.SelectProperties
) but the calculated properties have a value of null
, the only property that returns the correct value is name
:
$globalConfig = @{
PingAddress = '8.8.8.8';
SystemState = @{
Processors = @{
Namespace = 'root\cimv2';
ClassName = 'Win32_Processor';
SelectProperties = 'name', @{ n = 'CpuStatus'; e = { [int]$_.CpuStatus }}, @{ n = 'CurrentVoltage'; e = { [int]$_.CurrentVoltage }};
}
}
}
$job = Start-Job -Name Processors -ArgumentList $globalConfig.SystemState.Processors -ScriptBlock {
Try{
$Response = @{
State = @();
Error = $Null
}
$Response.State = Get-CimInstance -ClassName $Args[0].ClassName | Select-Object $Args[0].SelectProperties -ErrorAction Stop
}Catch{
$Response.Error = @{Id = 2; Message = "$($Args[0].Target) query failed: $($_.Exception.Message)"}
}
Return $Response
}
$job | Wait-Job
$job | Receive-Job | ConvertTo-Json -Depth 3
Id Name PSJobTypeName State HasMoreData Location Command
-- ---- ------------- ----- ----------- -------- -------
119 Processors BackgroundJob Completed True localhost ...
{
"Error": null,
"State": {
"name": "Intel(R) Core(TM) i7-4710MQ CPU @ 2.50GHz",
"CpuStatus": null,
"CurrentVoltage": null
}
}
Yet if I run the same job but with the same calculated properties hard coded (not passed to Select-Object using a PSObject), it works as expected (the values 1
and 12
are returned in the output):
$job = Start-Job -Name Processors -ArgumentList $globalConfig.SystemState.Processors -ScriptBlock {
Try{
$Response = @{
State = @();
Error = $Null
}
$Response.State = Get-CimInstance -ClassName $Args[0].ClassName | Select-Object Name, @{ n = 'CpuStatus'; e = { [int]$_.CpuStatus }},@{ n = 'CurrentVoltage'; e = { [int]$_.CurrentVoltage }}
}Catch{
$Response.Error = @{Id = 2; Message = "$($Args[0].Target) query failed: $($_.Exception.Message)"}
}
Return $Response
}
$job | Wait-Job
$job | Receive-Job | ConvertTo-Json -Depth 3
Id Name PSJobTypeName State HasMoreData Location Command
-- ---- ------------- ----- ----------- -------- -------
121 Processors BackgroundJob Completed True localhost ...
{
"Error": null,
"State": {
"Name": "Intel(R) Core(TM) i7-4710MQ CPU @ 2.50GHz",
"CpuStatus": 1,
"CurrentVoltage": 12
}
}
How can I pass an object of calculated properties in-line to Select-Object
while inside of a job?