在所有本地组中搜索特定用户ID

时间:2018-03-17 02:38:26

标签: powershell

我目前有这个脚本,除了我只想看到帐户所在的组的价值,而不是所有帐户的价值时,我只需要我所需要的一切。

    ForEach  ($Computer in  $Computername) {      
                        [ADSI]$S = "WinNT://$computer"
                        $S.children.where({$_.class -eq 'group'}) |
                        Select @{Name="Name";Expression={$_.name.value}},
                        @{Name="Members"; Expression={[ADSI]$group = "$($_.Parent)/$($_.Name),group" 
                        $members = $Group.psbase.Invoke("Members")
                        ($members| ForEach-Object { $_.GetType().InvokeMember("Name", 'GetProperty', $null, $_, $null ) | Select-String $account }) -join ";"  

                } 
                }
}

输出就像这样 -

Name                                Members     
----                                -------     
Access Control Assistance Operators             
Administrators                      USERNAME
Backup Operators                                
Certificate Service DCOM Access                 
Cryptographic Operators                         
Distributed COM Users                           
Event Log Readers                               
Guests                                          

我怎么能这样输出只是具有用户名的管理员组,没有其他组?

1 个答案:

答案 0 :(得分:1)

在输出中添加过滤器,以便仅显示包含成员的组| Where-Object { $_.Members },例如

ForEach  ($Computer in  $Computername) {      
    [ADSI]$S = "WinNT://$computer"
    $S.children.where({$_.class -eq 'group'}) |
    Select-Object @{Name="Name";    Expression={$_.name.value}},
                  @{Name="Members"; Expression={
                      [ADSI]$group = "$($_.Parent)/$($_.Name),group" 
                      $members = $Group.psbase.Invoke("Members")
                      ($members| ForEach-Object {
                        $_.GetType().InvokeMember("Name", 'GetProperty', $null, $_, $null ) | Select-String $account }) -join ";"  
                      }} | 
    Where-Object { $_.Members }
}