在 ForEach-Object 循环中使用作业收集数据
Collecting data with jobs inside a ForEach-Object loop
目标是拥有一个显示计算机($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 等)。
如果我这样做 "normally",没有工作,它工作正常但需要一段时间才能完成,因此我认为 运行 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
目标是拥有一个显示计算机($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 等)。
如果我这样做 "normally",没有工作,它工作正常但需要一段时间才能完成,因此我认为 运行 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