$clientid = "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" $clientsecret = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" $subscriptionid = "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" $tenantid = "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" $resource = "https://management.azure.com/" $tokenendpoint = "https://login.microsoftonline.com/$tenantid/oauth2/token" $body = @{ grant_type = "client_credentials" resource = $resource client_id = $clientid client_secret = $clientsecret } $tokenresponse = Invoke-RestMethod -Method Post -Uri $tokenendpoint -Body $body -ContentType "application/x-www-form-urlencoded" $accesstoken = $tokenresponse.access_token