Public/Users/Test-GraphUser.ps1
# Module: Orbit # Function: Support # Author: David Eberhardt # Updated: 27-MAY 2023 # Status: RC function Test-GraphUser { <# .SYNOPSIS Tests whether a User exists in Azure AD (record found) .DESCRIPTION Simple lookup - does the User Object exist - to avoid TRY/CATCH statements for processing .PARAMETER UserPrincipalName Mandatory. The sign-in address, User Principal Name or Object Id of the Object. .EXAMPLE Test-GraphUser -UserPrincipalName "$UPN" Will Return $TRUE only if the object $UPN is found. Will Return $FALSE in any other case, including if there is no Connection to Graph! .INPUTS System.String .OUTPUTS Boolean .NOTES x .COMPONENT SupportingFunction UserManagement .FUNCTIONALITY Tests whether an Azure Ad User exists in Graph .LINK https://github.com/DEberhardt/Orbit/tree/main/docs/Orbit.Users/Test-GraphUser.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/ #> [CmdletBinding()] [OutputType([Boolean])] param( [Parameter(Mandatory, Position = 0, ValueFromPipeline, ValueFromPipelineByPropertyName, HelpMessage = 'This is the UserID (UPN)')] [Alias('ObjectId', 'Identity')] [string[]]$UserPrincipalName ) #param begin { Show-OrbitFunctionStatus -Level RC Write-Verbose -Message "[BEGIN ] $($MyInvocation.MyCommand)" # Asserting Graph Connection if ( -not (Test-GraphConnection) ) { throw 'Connection to Microsoft Graph not established. Please validate connection' } } #begin process { foreach ($User in $UserPrincipalName) { Write-Verbose -Message "[PROCESS] $($MyInvocation.MyCommand) - Testing: '$User'" try { $UserObject = Get-MgUser -UserId "$User" -WarningAction SilentlyContinue -ErrorAction STOP if ( $null -ne $UserObject ) { return $true } else { return $false } } catch { return $false } } } #process end { Write-Verbose -Message "[END ] $($MyInvocation.MyCommand)" } #end } #Test-GraphUser |