我正在尝试将计算属性的哈希表传递给查询,以便与Select-Object
. 它在控制台中运行时有效。我可以确认该作业正在读取哈希表,因为它在结果中列出了选定的属性,但它们的所有值都是空的。
注意:我知道我不需要对这些属性进行类型转换。我只是展示我的问题。
如果我运行以下代码(看起来很奇怪,但实际上有一个用例),输出包含我选择的属性(来自$globalConfig.SystemState.Processors.SelectProperties
)但计算的属性的值为null
,唯一返回正确值的属性是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
}
}
然而,如果我运行相同的作业,但使用硬编码的相同计算属性(未使用 PSObject 传递给 Select-Object),它会按预期工作(值1
并12
在输出中返回):
$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
}
}
如何在Select-Object
工作中将计算属性的对象内联传递给?