G2A Many GEOs
SHARE
TWEET

Recursive Group Member

Yevrag35 Apr 6th, 2020 (edited) 279 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Function Get-RecursiveCloudGroups
  2. {
  3.  
  4.     [cmdletbinding()]
  5.  
  6.     param(
  7.  
  8.         [parameter(Mandatory = $True, ValueFromPipeline = $true)]
  9.         $CloudGroup,
  10.  
  11.         [Parameter(Mandatory = $False, DontShow = $True)]
  12.         [System.Collections.Generic.HashSet[guid]] $DontCheck
  13.     )
  14.  
  15.     Begin
  16.     {
  17.         If (-not(Get-AzureADCurrentSessionInfo))
  18.         {
  19.             Connect-AzureAD
  20.         }
  21.         $list = New-Object 'System.Collections.Generic.List[object]'
  22.         if (-not $PSBoundParameters.ContainsKey("DontCheck"))
  23.         {
  24.             $DontCheck = New-Object 'System.Collections.Generic.HashSet[guid]'
  25.         }
  26.     }
  27.  
  28.     Process
  29.     {
  30.         $members = @(Get-AzureADGroupMember -ObjectId $CloudGroup.ObjectId)
  31.  
  32.         $groups, $users = $members.Where({$_.ObjectType -eq "Group"}, "Split")
  33.  
  34.         if ($groups.Count -gt 0)
  35.         {
  36.             foreach ($g in $groups.Where({$DontCheck -notcontains $_.ObjectId}))
  37.             {
  38.                 [void]$DontCheck.Add($g.ObjectId)
  39.                 $recurseCheck = Get-RecursiveCloudGroups -CloudGroup $g -DontCheck $DontCheck
  40.  
  41.                 $DontCheck.UnionWith($recurseCheck.DontCheck)
  42.  
  43.                 foreach ($mm in $recurseCheck.Members.Where({-not $list.Contains($_)}))
  44.                 {
  45.                     $list.Add($mm)
  46.                 }
  47.             }
  48.         }
  49.         if ($users.Count -gt 0)
  50.         {
  51.             foreach ($u in $users.Where({-not $list.Contains($_)}))
  52.             {
  53.                 $list.Add($u)
  54.             }
  55.         }
  56.     }
  57.     End
  58.     {
  59.         if ($PSBoundParameters.ContainsKey("DontCheck"))
  60.         {
  61.             [pscustomobject]@{
  62.                 Members = $list
  63.                 DontCheck = $DontCheck
  64.             }
  65.         }
  66.         else
  67.         {
  68.             $list
  69.         }
  70.     }
  71. }
RAW Paste Data
Ledger Nano X - The secure hardware wallet
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top