2017-12-08 4 views
0

私はかなり大きな監査プロジェクトを手に入れました。powershellはグループのメンバーを再帰的に検索します

グループのメンバーであるすべてのユーザーの名前、SamAccountName、Title、Departmentを取得する必要があります。問題は、グループにグループがあり、グループ内にグループがあることです。別の問題は、すべてのグループの約99%がアスタリスクを表示名(NOT SamAccountName)に持つことです。

ここには私が現在持っているコードがあります。それはその名前にアスタリスクが付いたグループを受け取るまでうまくいきます(したがって、.Replace( "*"、 "")部分...誰でもアイデアを得ましたBill_Stewartが言ったように?

function Get-NestedGroupMember { 
[CmdletBinding()] 
param(
    [Parameter(Mandatory)] 
    [string]$Group 
) 
$broke = @(); 
## Find all members in the group specified 
$members = Get-ADGroupMember -Identity $Group 
foreach ($member in $members){ 
    ## If any member in that group is another group just call this function again 
    if ($member.objectClass -eq 'group'){ 
     $memberGroup = $($member.Name).Replace("*", "") 
     try{ 
      Get-NestedGroupMember -Group "$($memberGroup)" 
     }catch{ 
      $broke += "$($memberGroup)`n" 
     } 
    }else{ 
     ## otherwise, just output the non-group object (probably a user account) 
     $member.Name 
    } 
} 
Write-Host "`nThe following groups could not be found automatically.`n`n$($broke)" 
} 

$getGroup = Read-Host -Prompt "Group name" 

Get-NestedGroupMember $getGroup 
+1

を使用して、あなたがするので、 '-Recursive'パラメータを使用していません... –

答えて

1

をこの問題を解決する方法について、あなたはすでにあなたのために存在する機能を実装しようとしている。Get-ADGroupMemberは、ネストされたグループのすべてを検索し、あなたのためのメンバーを取得します-Recursiveパラメータがあります。出力を選択にパイプし、気になるプロパティのみを取得することができます。

AD: 
    TopGroup 
    Betty 
    Bob 
    NestedGroup1 
     Joe 
     Frank 
    NestedGroup2 
     George 
     Herman 

-Recursive

Get-AdGroupMember TopGroup -Recursive | Select-Object SamAccountName 
Betty 
Bob 
Joe 
Frank 
George 
Herman 
関連する問題