Functions/Public/Get-MsGDirectoryObject.ps1
|
function Get-MsGDirectoryObject { <# .SYNOPSIS Retrieves properties of a directory object. .DESCRIPTION This function retrieves the properties of a specified directory object. .PARAMETER ObjectId ObjectId of the directory object. .PARAMETER Headers Authentication Headers to connect to Microsoft Graph. .PARAMETER Jwt Jwt to connect to Microsoft Graph. .EXAMPLE PS> Get-MsGDirectoryObject -Id 36af72f2-a273-45e6-9acf-ad6dcfdfc585 Retrieves properties for the directory object '36af72f2-a273-45e6-9acf-ad6dcfdfc585'. #> [CmdletBinding(DefaultParameterSetName = 'Name')] param( [parameter(Mandatory = $true, Position = 0, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)][Alias('ObjectId')][string]$Id, [parameter(Mandatory = $false)][hashtable]$Headers, [parameter(Mandatory = $false)][string]$Jwt ) BEGIN { # Session Checks if ([string]::IsNullOrEmpty($Headers) -and [string]::IsNullOrEmpty($Jwt)) { if ($null -ne $myGraphToken) { Write-Verbose "Setting Jwt to Global: $myGraphToken" $Jwt = $myGraphToken } } if ([string]::IsNullOrEmpty($Headers) -and -not([string]::IsNullOrEmpty($Jwt))) { $Headers = @{ 'Authorization' = 'Bearer {0}' -f $Jwt 'Content-Type' = 'application/json' } } if ([string]::IsNullOrEmpty($Headers)) { $errorMessage = '{0} : Unable to run command. Headers are empty.' -f $MyInvocation.MyCommand.Name throw $errorMessage } # end Session Checks # Function Endpoint $baseEndpoint = 'directoryObjects' # end region } PROCESS { # Retrieve results. try { $graphEndpoint = '{0}/{1}' -f $baseEndpoint, $Id $irmResponse = Invoke-MsGRequest -Method 'Get' -Endpoint $graphEndpoint -Version 'v1.0' -Headers $Headers -ErrorAction 'Stop' Write-Output $irmResponse } catch { $errorMessage = Get-MsGErrorMessage $_ $errorException = '{0} : Unable to retrieve {1} from Entra ID. Error: {2}' -f $MyInvocation.MyCommand.Name, $baseEndpoint, $errorMessage throw $errorException } # end region } } |