2017-12-08 12 views
0

나는 자동화하기를 바라고있는 꽤 큰 감사 프로젝트를 가지고 있습니다.powershell 그룹의 구성원을 재귀 적으로 검색합니다.

그룹의 일부인 모든 사용자 이름, SamAccountName, Title 및 Department를 얻어야합니다. 문제는 그룹에 그룹이 있고 그룹 내에 그룹이 있다는 것입니다. 또 다른 문제점은 모든 그룹의 약 99 %가 표시 이름 (별칭 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