Tuesday, 7 January 2014

Powershell - Query Group Membership in another Domain

This in one way that I know of to search another Domain by mounting it as a drive (I'm sure there are different ways of skinning a cat but this technique would work for a number of Domains - again I'm sure there are further options to be able to the same thing!!).

## Query Group Membership ##
Import-Module ActiveDirectory

## Mount AD as a drive ##
New-PSDrive -Name OtherDomain -PSProvider ActiveDirectory -Root 'DC=other,DC=domain,DC=co,DC=uk' -Server dc1.other.domain.co.uk -Credential DOMAIN\account
cd OtherDomain:
Get-ADGroup -Filter {Name -eq "ADGroup"} |
     ForEach-Object{
          $hash = @{GroupName = $_.Name ; UserID = '' ; Member = ''}
          $_ | Get-ADGroupMember -ea 0 -recurs |
              ForEach-Object{
                   $hash.Member = $_.Name
                   $hash.UserID = $_.SamAccountName
                   New-Object psObject -Property $hash
              }
          } | sort groupname, member | Export-CSV "C:\Data\ADGroup.csv" –NoTypeInformation

No comments:

Post a Comment