functions/azure/New-ApplicationWithSecret.ps1

function New-ApplicationWithSecret {
    param (
        [Parameter(Mandatory = $true)]
        [string]$DisplayName
    )

    $headers = @{
        Authorization = Get-RequestHeaderAuthorization -RequestUri 'https://graph.microsoft.com'
        "Content-Type" = "application/json"
    }

    $appPayload = @{ displayName = $DisplayName } | ConvertTo-Json -Depth 3
    $appResponse = Invoke-RestMethod -Method POST -Uri "https://graph.microsoft.com/v1.0/applications" -Headers $headers -Body $appPayload

    if (-not $appResponse.id) {
        throw "Fehler beim Erstellen der App."
    }

    $secretPayload = @{
        passwordCredential = @{ displayName = "InitialSecret" }
    } | ConvertTo-Json -Depth 3

    $secretResponse = Invoke-RestMethod -Method POST `
        -Uri "https://graph.microsoft.com/v1.0/applications/$($appResponse.id)/addPassword" `
        -Headers $headers -Body $secretPayload

    return [PSCustomObject]@{
        ApplicationId     = $appResponse.appId
        ObjectId          = $appResponse.id
        ClientSecret      = $secretResponse.secretText
        DisplayName       = $appResponse.displayName
    }
}