0

在这种情况下,我的工作正常,但我不禁觉得必须有一种更快的方法来做到这一点。我在这里怀疑我的结构。您是否看到了一种更快的方法可以做到这一点?这foreach需要一段时间,因为它必须查询很多次。目标是拥有一个显示计算机 ( $poste) 集合(名称和对象路径)的数组。

$poste = "p1234"

$SiteCode = "PRX"
$SiteServer = "SSX"

$ResourceID = Get-WmiObject -Namespace root\sms\site_$SiteCode -Computer $SiteServer -Class SMS_R_SYSTEM -Filter "Name='$poste'" |
              Select-Object -ExpandProperty ResourceID
$CollectionIDs = Get-WmiObject -Namespace root\sms\site_$SiteCode -ComputerName $SiteServer -Class sms_fullcollectionmembership -Filter "ResourceID ='$resourceID'" |
                 Select-Object -expand CollectionID

foreach ($CollectionID in $CollectionIDs) {
    $query = @"
SELECT * FROM SMS_Collection
WHERE SMS_Collection.CollectionID='$CollectionID'
"@

[array]$CollectionNamesPath += Get-WmiObject -Namespace root\sms\site_$SiteCode -Computer $SiteServer -Query $query |
                               Select-Object name,ObjectPath |
                               Sort-Object -Property Name
} 

$CollectionNamesPath | Out-GridView
4

1 回答 1

1

这可能是您现有代码的最干净的版本:

$poste = "p1234"
$SiteCode = "PRX"
$SiteServer = "SSX"
$ResourceID = Get-WmiObject -Namespace root\sms\site_$SiteCode -ComputerName $SiteServer -Class SMS_R_SYSTEM -Filter "Name='$poste'" -Property ResourceID | Select-Object -ExpandProperty ResourceID
$CollectionIDs = Get-WmiObject -Namespace root\sms\site_$SiteCode -ComputerName $SiteServer -Class sms_fullcollectionmembership -Filter "ResourceID='$resourceID'" -Property CollectionID | Select-Object -ExpandProperty CollectionID

$CollectionNamesPath = foreach ($CollectionID in $CollectionIDs) {
    $Query = "Select Name, ObjectPath From SMS_Collection Where SMS_Collection.CollectionID='$CollectionID'"
    Get-WmiObject -Namespace root\sms\site_$SiteCode -ComputerName $SiteServer -Query $Query | Select-Object Name, ObjectPath | Sort-Object -Property Name
} 

$CollectionNamesPath | Out-GridView

你也可以试试这个,虽然我不确定 WQL 对它的支持程度:

$poste = "p1234"
$SiteCode = "PRX"
$SiteServer = "SSX"
$ResourceID = Get-WmiObject -Namespace root\sms\site_$SiteCode -ComputerName $SiteServer -Class SMS_R_SYSTEM -Filter "Name='$poste'" -Property ResourceID | Select-Object -ExpandProperty ResourceID
$CollectionIDs = Get-WmiObject -Namespace root\sms\site_$SiteCode -ComputerName $SiteServer -Class sms_fullcollectionmembership -Filter "ResourceID='$resourceID'" -Property CollectionID | Select-Object -ExpandProperty CollectionID

$Query = "Select CollectionID, Name, ObjectPath From SMS_Collection Where" + $($($CollectionIDs | ForEach-Object { " SMS_Collection.CollectionID='$_' " }) -join 'or')

$CollectionNamesPath = Get-WmiObject -Namespace root\sms\site_$SiteCode -ComputerName $SiteServer -Query $Query |
    Sort-Object -Property CollectionID, Name |
    Select-Object Name, ObjectPath

$CollectionNamesPath | Out-GridView

也许可以执行以下操作:

Get-CimInstance -Namespace root\sms\site_$SiteCode -ComputerName $SiteServer -Class SMS_R_SYSTEM -Filter "Name='$poste'" -KeyOnly |
    Get-CimAssociatedInstance -ComputerName $SiteServer -ResultClassName sms_fullcollectionmembership -KeyOnly |
    Get-CimAssociatedInstance -ComputerName $SiteServer -ResultClassName SMS_Collection |
    Sort-Object -Property CollectionID, Name |
    Select-Object Name, ObjectPath

当它起作用时,它太棒了。但是,根据我Get-CimAssociatedInstance的经验,充其量是一种不平衡的体验,在远程计算机上工作时更是如此。

于 2019-02-22T15:21:03.747 回答