Public/AzureAD/Get-MsGraphToken.ps1

function Get-MsGraphToken
{
    [CmdletBinding(PositionalBinding = $false)]
    param
    (
        [Alias('client_id')][Parameter(Mandatory = $true)][string]$ClientId,
        [Alias('client_secret')][Parameter(Mandatory = $true)][string]$ClientSecret,
        [Alias('tenant_id')][Parameter(Mandatory = $true)][string]$TenantId,
        [Parameter(Mandatory = $false)][string]$Scope = "https://graph.microsoft.com/.default",
        [Parameter(Mandatory = $false)][string]$GrantType = "client_credentials"
        
    )

    $uri = "https://login.microsoftonline.com/$TenantId/oauth2/v2.0/token"

    $body = @{
        client_id     = $ClientId
        scope         = $Scope
        client_secret = $ClientSecret
        grant_type    = $GrantType
    }

    try
    {
        $response = Invoke-RestMethod -Method Post -Uri $uri -ContentType "application/x-www-form-urlencoded" -Body $body -UseBasicParsing
        Write-Log "Successfully retrived MS Graph API token."
        return $response
    }
    catch
    {
        Write-Log "Error getting MS Graph API token." -LogType: error -ErrorObject $_
        Write-Error $_
    }
    
}