2014-07-23 3 views
1

각 그룹의 구성원과 함께 Active Directory의 그룹 목록을 함께 컴파일하는 PowerShell 스크립트를 작성하려고합니다. 나의 궁극적 인 목표는 CSV 파일이 밖으로 내보내는 것입니다, 그래서 최종 PowerShell을 다차원 배열은 다음과 같은 형식으로 갖고 싶어 : 나는 시도하고이 작업을 수행하려면 다음 코드를 사용하고PowerShell 배열/추가 구성원에 데이터 추가

GroupName   GroupMember 
Domain Admins  Henry Doe 
Domain Admins  Melody Doe 
Domain Names  Doe Ray Me 
Domain Users  John Doe 
Domain Users  Jane Doe 
(etc…) 

:

[array]$arrGroupMemberList = New-Object PSObject 
Add-Member -InputObject $arrGroupMemberList -membertype NoteProperty -Name 'GroupName' -Value "" 
Add-Member -InputObject $arrGroupMemberList -membertype NoteProperty -Name 'GroupMember' -Value "" 
[array]$arrGroupMemberList = @() 

[array]$arrGroupNameObjects = Get-ADGroup -Filter * | Where-Object {$_.Name -Like "Domain*"} 

If ($arrGroupNameObjects.Count -ge 1) 
    { 
    ## Cycle thru each group name and get the members 
    $arrGroupNameObjects | ForEach-Object { 
     [string]$strTempGroupName = $_.Name 
     $arrGroupMemberObjects = Get-ADGroupMember $strTempGroupName -Recursive 
     If ($arrGroupMemberObjects.Count -ge 1) 
      { 
      ## Cycle thru the group members and compile into the final array 
      $arrGroupMemberObjects | ForEach-Object { 
       $arrGroupMemberList += $strTempGroupName, $_.Name 
      } 
      } 
    } 
    } 

내 문제는, 내가 내 배열로 다음과 같이 끝나는 계속 :

Domain Admins 
Henry Doe 
Domain Admins 
Melody Doe 
Domain Names 
Doe Ray Me 
Domain Users 
John Doe 
Domain Users 
Jane Doe 

나는 몇 가지 방법을 시도하고 내가 검색 한하지만 어디 답을 발견하지 않았습니다. 나는 그것이 단순한 것이라고 확신하지만 잘못된 것은 무엇인가? 해야 할 일과 같이 필요한 데이터로 다차원 배열을 만들 수 있습니까? 내가 사용하는 경우 대신 다음

  ## Cycle thru the group members and compile into the final array 
      $arrGroupMemberObjects | ForEach-Object { 
       $arrGroupMemberList[$intIndex].GroupName = $strTempGroupName 
       $arrGroupMemberList[$intIndex].GroupMember = $_.Name 
       $intIndex++ 

내가 좋아하는 오류와 끝까지 :

Property 'GroupMember' cannot be found on this object; make sure it exists and is settable. 
Property 'GroupName' cannot be found on this object; make sure it exists and is settable. 

감사

는 ** UPDATE **

나는 밖으로 어디에서 발견 할 수 있습니다 내 문제는 배열 구성원을 추가 할 때 수 있습니다. 내 PowerShell 스크립트의 끝에서, 나는 다음 코드 줄 추가하고 :

$arrGroupMemberList | Get-Member 

더 속성이 없습니다를 내 요소는 앞서 스크립트에 추가-Member cmdlet으로 그들을 추가에도 불구하고,이 없습니다. Add-Member cmdlet을 제대로 사용하고 있습니까?

답변

1

는 내가 잘못하고 있었는지 알아 냈어 - 나는 잘못-회원을 추가 을 사용하고 있었다. Add-Member을 사용하여 컬렉션에 구성원을 추가하려고 시도했지만 그런 식으로 작동하지 않습니다. 단순한 것이지만 어디서나 논의 된 것을 볼 수 없었습니다. 그래서 몇 가지 예를 발견하고 시행 착오를하고 작동하도록했습니다. 다른 사람들이 같은 문제가있는 경우에 대비하여 여기에 다시 업데이트를 게시하고 싶습니다. 다음 코드는 내가 원하는 것처럼 작동합니다 (각 그룹의 그룹 구성원과 함께 Active Directory에서 그룹 목록을 가진 배열을 만듭니다) :

[array]$arrGroupNameObjects = Get-ADGroup -Filter * | Where-Object {$_.Name -Like "Domain*"} 

If ($arrGroupNameObjects.Count -ge 1) 
    { 
    ## Cycle thru each group name and get the members 
    $arrGroupNameObjects | ForEach-Object { 
     [string]$strTempGroupName = $_.Name 
     $arrGroupMemberObjects = Get-ADGroupMember $strTempGroupName -Recursive 
     If ($arrGroupMemberObjects.Count -ge 1) 
      { 
      ## Cycle thru the group members and compile into the final array 
      $arrGroupMemberObjects | ForEach-Object { 
       $objGroupMember = New-Object PSObject 
       Add-Member -InputObject $objGroupMember -membertype NoteProperty -Name 'GroupName' -Value $strTempGroupName 
       Add-Member -InputObject $objGroupMember -membertype NoteProperty -Name 'GroupMemberName' -Value $_.Name 
       [array]$arrGroupMemberList += $objGroupMember 
      } 
      } 
    } 
    }