目标是要有一个对象来显示计算机的($poste
)收集信息。我正在尝试通过作业收集该数据,以便其运行更快。但是,我正在努力做到正确。我正在尝试:
$poste = "xx"
$SiteCode = "XX"
$SiteServer = "XX"
$ResourceID = Get-WmiObject -Namespace root\sms\site_$SiteCode -Computer $SiteServer -Class SMS_R_SYSTEM -Filter "Name='$poste'" |
Select-Object -ExpandProperty ResourceID
$NomsCollection = Get-WmiObject -Namespace root\sms\site_$SiteCode -ComputerName $SiteServer -Class sms_fullcollectionmembership -Filter "ResourceID ='$resourceID'" |
ForEach-Object {
Invoke-Command -ScriptBlock {
Get-WmiObject -Namespace root\sms\site_$SiteCode -Computer $SiteServer -Class SMS_COllection -Filter "CollectionID='$($_.CollectionID)'"
} -Computer localhost -AsJob
Get-Job | Receive-Job
}
$NomsCollection |
Select-Object CollectionID, Name, Comment, ObjectPath |
Sort-Object -Property Name |
Out-GridView -Title $poste -Wait
我在gridview中最后得到的只是作业的名称(job1,job3等)。
如果我“正常”地执行此操作而不执行任何作业,则可以正常工作,但需要一段时间才能完成,因此我的想法是将ForEach-Object
中的命令作为作业运行。
例如,如果执行此操作,我将得到想要的结果:
$poste = "xx"
$SiteCode = "xx"
$SiteServer = "xx"
$ResourceID = Get-WmiObject -Namespace root\sms\site_$SiteCode -computer $SiteServer -Class SMS_R_SYSTEM -Filter "Name='$poste'" |
Select-Object -ExpandProperty ResourceID
$NomsCollection = Get-WmiObject -Namespace root\sms\site_$SiteCode -ComputerName $SiteServer -Class sms_fullcollectionmembership -Filter "ResourceID ='$resourceID'" |
ForEach-Object {
Get-WmiObject -Namespace root\sms\site_$SiteCode -Computer $SiteServer -Class SMS_COllection -Filter "CollectionID='$($_.CollectionID)'"
}
$NomsCollection |
Select-Object CollectionID, Name, Comment, ObjectPath |
Sort-Object -Property Name |
Out-GridView -Title $poste -Wait
在处理ForEach-Object
中的工作时我缺少什么?
编辑:感谢下面的回答,这是我使用的最终代码,略有不同:
$SiteCode = "xx"
$SiteServer = "xx"
$ResourceID = Get-WmiObject -Namespace root\sms\site_$SiteCode -computer $SiteServer -Class SMS_R_SYSTEM -Filter "Name='$poste'" |
Select-Object -ExpandProperty ResourceID
$jobs = Get-WmiObject -Namespace root\sms\site_$SiteCode -ComputerName $SiteServer -Class sms_fullcollectionmembership -Filter "ResourceID ='$resourceID'" |
ForEach-Object {
Start-Job -ArgumentList $SiteServer, $SiteCode, $_.CollectionID -ScriptBlock {
Param ($server,$code,$id)
Get-WmiObject -Namespace root\sms\site_$code -Computer $server -Class SMS_COllection -Filter "CollectionID='${id}'"
}
}
$NomsCollection = $jobs | Get-Job | Wait-Job | Receive-Job
$NomsCollection | Select-Object CollectionID, Name, Comment, ObjectPath | Sort-Object -Property Name | Out-GridView -Title $poste -Wait
答案 0 :(得分:1)
您需要将作业外部定义的变量传递给作业:
Invoke-Command -ScriptBlock {
Param($server, $code, $id)
Get-WmiObject -Namespace root\sms\site_$code -Computer $server -Class SMS_COllection -Filter "CollectionID='${id}'"
} -Computer localhost -ArgumentList $SiteServer, $SiteCode, $_.CollectionID -AsJob
此外,您需要等待作业完成才能收集输出:
$jobs = Get-WmiObject -Namespace ... | ForEach-Object {
Invoke-Command ...
}
$NomsCollection = $jobs | Get-Job | Wait-Job | Receive-Job