Public/Test-GraphGroup.ps1
# Module: Orbit # Function: Support # Author: David Eberhardt # Updated: 27-May 2023 # Status: Beta function Test-GraphGroup { <# .SYNOPSIS Tests whether an Group exists in Azure AD (record found) .DESCRIPTION Simple lookup - does the Group Object exist - to avoid TRY/CATCH statements for processing .PARAMETER Identity Mandatory. The Name or User Principal Name (MailNickName) of the Group to test. .EXAMPLE Test-GraphGroup -Identity "My Group" Will Return $TRUE only if the object "My Group" is found. Will Return $FALSE in any other case .INPUTS System.String .OUTPUTS Boolean .NOTES None .COMPONENT SupportingFunction UserManagement .FUNCTIONALITY Tests whether an Group exists in Graph .LINK https://github.com/DEberhardt/Orbit/tree/main/docs/Orbit.Groups/Test-GraphGroup.md .LINK https://github.com/DEberhardt/Orbit/tree/main/docs/about/about_UserManagement.md .LINK https://github.com/DEberhardt/Orbit/tree/main/docs/about/about_Supporting_Functions.md .LINK https://github.com/DEberhardt/Orbit/tree/main/docs/ #> [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidGlobalVars', '', Justification = 'Required for performance. Removed with Disconnect-Me')] [CmdletBinding()] [OutputType([Boolean])] param( [Parameter(Mandatory, Position = 0, ValueFromPipeline, HelpMessage = 'This is the Name or UserPrincipalName of the Group')] [Alias('UserPrincipalName', 'GroupName')] [string]$Identity ) #param begin { Show-OrbitFunctionStatus -Level Beta Write-Verbose -Message "[BEGIN ] $($MyInvocation.MyCommand)" # Asserting Graph Connection if ( -not (Test-GraphConnection) ) { throw 'Connection to Microsoft Graph not established. Please validate connection' } # Loading all Groups if ( -not $global:OrbitQueryTenantGraphGroups) { Write-Verbose -Message 'Groups not loaded yet, depending on the size of the Tenant, this will run for a while!' -Verbose $global:OrbitQueryTenantGraphGroups = Get-MgGroup -All -WarningAction SilentlyContinue -ErrorAction SilentlyContinue } } #begin process { Write-Verbose -Message "[PROCESS] $($MyInvocation.MyCommand)" $CallTarget = $null $CallTarget = Get-MgGroup -Filter "Displayname eq '$Identity'" -WarningAction SilentlyContinue -ErrorAction SilentlyContinue $CallTarget = $CallTarget | Where-Object Displayname -EQ "$Identity" if ( -not $CallTarget ) { try { $CallTarget = Get-MgGroup -GroupId "$Identity" -WarningAction SilentlyContinue -ErrorAction Stop } catch { #TODO Test by MailNickName/ if ( $script:OrbitRegexUPN.isMatch($Identity) ) { $CallTarget = $global:OrbitQueryTenantGraphGroups | Where-Object Mail -EQ "$Identity" -WarningAction SilentlyContinue -ErrorAction SilentlyContinue } elseif ( $script:OrbitRegexGuid.isMatch($Identity) ) { $CallTarget = $global:OrbitQueryTenantGraphGroups | Where-Object Id -EQ "$Identity" -WarningAction SilentlyContinue -ErrorAction SilentlyContinue } else { $CallTarget = $global:OrbitQueryTenantGraphGroups | Where-Object DisplayName -EQ "$Identity" -WarningAction SilentlyContinue -ErrorAction SilentlyContinue } } } #CHECK Test for ONE object? if ($CallTarget) { return $true } else { return $false } } #process end { Write-Verbose -Message "[END ] $($MyInvocation.MyCommand)" } #end } #Test-GraphGroup |