functions/azure/Get-Application.ps1

function Get-Application {
    param (
        [Parameter(Mandatory = $true, ParameterSetName = "ByDisplayName")]
        [string]$DisplayName,
        [Parameter(Mandatory = $true, ParameterSetName = "ByAppId")]
        [guid]$AppId,
        [Parameter(Mandatory = $true, ParameterSetName = "ById")]
        [guid]$Id
    )

    $uri = if ($PSCmdlet.ParameterSetName -eq "ByDisplayName") {
        "https://graph.microsoft.com/v1.0/applications?`$filter=displayName eq '$DisplayName'"
    } elseif ($PSCmdlet.ParameterSetName -eq "ByAppId") {
        "https://graph.microsoft.com/v1.0/applications?`$filter=appId eq '$([string]$AppId)'"
    } else {
        "https://graph.microsoft.com/v1.0/applications/$Id"
    }

    $headers = @{
        Authorization = Get-RequestHeaderAuthorization -RequestUri $uri
        "Content-Type" = "application/json"
    }

    try {
        $response = Invoke-RestMethod -Method GET -Uri $uri -Headers $headers
        if ($PSCmdlet.ParameterSetName -eq "ByDisplayName" -or $PSCmdlet.ParameterSetName -eq "ByAppId") {
            if ($response.value.Count -eq 0) {
                Write-Error "No application found with display name '$DisplayName'."
                return $null
            }
            if ($response.value.Count -gt 1) {
                Write-Warning "Multiple applications found with display name '$DisplayName'!"
            }
            return $response.value[0]
        } else {
            return $response
        }
    } catch {
        Write-Error "Failed to retrieve application: $_"
        return $null
    }
}