Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Function Get-RecursiveCloudGroups
- {
- [cmdletbinding()]
- param(
- [parameter(Mandatory = $True, ValueFromPipeline = $true)]
- $CloudGroup,
- [Parameter(Mandatory = $False, DontShow = $True)]
- [System.Collections.Generic.HashSet[guid]] $DontCheck
- )
- Begin
- {
- If (-not(Get-AzureADCurrentSessionInfo))
- {
- Connect-AzureAD
- }
- $list = New-Object 'System.Collections.Generic.List[object]'
- if (-not $PSBoundParameters.ContainsKey("DontCheck"))
- {
- $DontCheck = New-Object 'System.Collections.Generic.HashSet[guid]'
- }
- }
- Process
- {
- $members = @(Get-AzureADGroupMember -ObjectId $CloudGroup.ObjectId)
- $groups, $users = $members.Where({$_.ObjectType -eq "Group"}, "Split")
- if ($groups.Count -gt 0)
- {
- foreach ($g in $groups.Where({$DontCheck -notcontains $_.ObjectId}))
- {
- [void]$DontCheck.Add($g.ObjectId)
- $recurseCheck = Get-RecursiveCloudGroups -CloudGroup $g -DontCheck $DontCheck
- $DontCheck.UnionWith($recurseCheck.DontCheck)
- foreach ($mm in $recurseCheck.Members.Where({-not $list.Contains($_)}))
- {
- $list.Add($mm)
- }
- }
- }
- if ($users.Count -gt 0)
- {
- foreach ($u in $users.Where({-not $list.Contains($_)}))
- {
- $list.Add($u)
- }
- }
- }
- End
- {
- if ($PSBoundParameters.ContainsKey("DontCheck"))
- {
- [pscustomobject]@{
- Members = $list
- DontCheck = $DontCheck
- }
- }
- else
- {
- $list
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement