Powershell - 过滤管道中可变性的结果

时间:2016-02-29 16:42:51

标签: powershell

我想将下面$ Groups变量的结果限制为仅返回特定组的成员。此代码检索计算机所属的组。我只对那些包含“SCCM”的AD组成员感兴趣。

我尝试过第5行,但这不起作用。这样做的正确方法是什么?感谢。

    $SamAccountNames = (Get-ADComputer Computer).SamAccountName
    $OSInfo = Get-WmiObject -Class Win32_OperatingSystem -ComputerName Computer
    $ServerGroupMemberShipList = Get-ADPrincipalGroupMembership $SAMAccountNames | Sort SAMAccountName 
    $Groups = $ServerGroupMemberShipList.Name 
    #$Groups = $ServerGroupMemberShipList.Name | Select {$ServerGroupMemberShipList.Name -like "SCCM"}
    Write-Host " "
    Write-Host "Checking Software Updates Compliance on" $Computer.ToUpper() -ForegroundColor Yellow -NoNewline
    Write-Host $OSInfo.Caption
    $Groups 

1 个答案:

答案 0 :(得分:1)

不要使用Select-Object cmdlet,而是尝试使用Where-Object cmdlet进行过滤,或者使用?$_变量是cmdlet正在处理的当前对象。

$Groups = $ServerGroupMemberShipList | Where-Object { $_.Name -like "SCCM" }

$Groups = $ServerGroupMemberShipList | ? { $_.Name -like "SCCM" }