Advertisement
Old-Lost

Get-GroupMember

Jun 22nd, 2017
2,850
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function ConvertFrom-FCP {
  2.     [CmdletBinding()][OutputType([Microsoft.ActiveDirectory.Management.ADObject])]
  3.     param (
  4.         [Parameter(Mandatory, Position = 0, ValueFromPipeline, ValueFromPipelineByPropertyName)][string]$DistinguishedName
  5.     )
  6.     begin {
  7.         if (-not (Get-Variable -Name FCPMa[p] -Scope Script)) { [hashtable]$script:FCPMap = @{ } }
  8.     }
  9.     process {
  10.         trap {
  11.             $script:FCPMap[$SID] = $null
  12.             return
  13.         }
  14.         [string]$SID = $DistinguishedName -replace '\ACN=(S-[-0-9]+).*', '$1'
  15.         if ($script:FCPMap.ContainsKey($SID)) {
  16.             if ($script:FCPMap[$SID]) { $script:FCPMap[$SID] }
  17.         } else {
  18.             [string]$SAM = [System.Security.Principal.SecurityIdentifier]::new($SID).Translate([System.Security.Principal.NTAccount]).Value
  19.             $Domain, $SamAccountName = $SAM -split '\\'
  20.             $SamAccountName = $SamAccountName -replace '\A ', '\20'
  21.             $script:FCPMap[$SID] = Get-ADObject -Filter 'SamAccountName -eq $SamAccountName' -Server $Domain -Property SamAccountName -ErrorAction Ignore
  22.             if ($script:FCPMap[$SID]) { $script:FCPMap[$SID] }
  23.         }
  24.     }
  25. } # function ConvertFrom-FCP
  26.  
  27. filter Skip-Null { $_ | ? { $_ -ne $null } }
  28.  
  29. function Get-GroupMember {
  30.     # Gets AD group membership
  31.     [CmdletBinding()]
  32.     param (
  33.         [Parameter(Mandatory, Position = 0)]$Identity,
  34.         [Parameter(Position = 1)][string]$Server
  35.     )
  36.     if (-not (Get-Variable -Name DNMa[p] -Scope Script)) { [hashtable]$Script:DNMap = @{ } }
  37.     $GetADGroupParams = @{ Identity = $Identity; ErrorAction = 'Stop' }
  38.     $GetADObjectParams = @{ ErrorAction = 'Ignore'; Property = 'SamAccountName' }
  39.     if ($PSBoundParameters.ContainsKey('Server')) {
  40.         $GetADGroupParams.Server = $Server
  41.         $GetADObjectParams.Server = $Server
  42.     }
  43.     try {
  44.         # Get-ADGroupMember will fail if the group contains any ForeignSecurityPrincipal objects
  45.         $Members = Get-ADGroupMember @GetADGroupParams
  46.     } catch [Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException] {
  47.         throw
  48.     } catch {
  49.         # Write-Verbose "Get-ADGroupMember failed. Falling back to Get-ADGroup -Property Member..."
  50.         $Members = Get-ADGroup @GetADGroupParams -Properties Member | Select-Object -ExpandProperty Member | % {
  51.             trap { return }
  52.             if (-not $DNMap.ContainsKey($_)) {
  53.                 $DNMap[$_] = Get-ADObject -Identity $_ @GetADObjectParams
  54.             }
  55.             $DNMap[$_]
  56.         } | Skip-Null
  57.     }
  58.     $Members = $Members | % {
  59.         [PSCustomObject]@{
  60.             SamAccountName = $_ | Select-Object SamAccountName | Select-Object -ExpandProperty SamAccountName
  61.             DistinguishedName = $_.DistinguishedName
  62.             ObjectClass = $_.ObjectClass
  63.         }
  64.     }
  65.     # The following is an attempt to prevent the FCP process from taking too long, as can happen
  66.     # when the domain containing the account is not responding.
  67.     # Set the limit to 2 seconds per lookup, which should be more than enough
  68.     $FCPMaxSeconds = [System.Math]::Min(((@($Members | ? ObjectClass -EQ 'foreignSecurityPrincipal').Count + 1) * 2), 150)
  69.     $Timer = [System.Diagnostics.Stopwatch]::StartNew()
  70.     $Members | ? ObjectClass -EQ 'foreignSecurityPrincipal' -pv One |
  71.     ? { $Timer.Elapsed.TotalSeconds -le $FCPMaxSeconds } |
  72.     ConvertFrom-FCP | % {
  73.         $One.SamAccountName = $_ | Select-Object SamAccountName | Select-Object -ExpandProperty SamAccountName
  74.         $One.DistinguishedName = $_.DistinguishedName
  75.         $One.ObjectClass = $_.ObjectClass
  76.     }
  77.     $Members | ? ObjectClass -NE 'foreignSecurityPrincipal'
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement