0

目标是拥有一个显示计算机 ( $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-Objectas 作业中运行命令。

例如,如果我这样做,我会得到我想要的结果:

$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
4

1 回答 1

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
于 2019-02-25T18:18:56.097 回答