目标是拥有一个显示计算机($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
您需要将作业外定义的变量传递到作业中:
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