Azure Rest-API Calls from PowerShell

Here is a simple Rest-API call from PowerShell for creating the resource group, using the App Registration:

 

# Set the Azure AD tenant ID, client ID, and client secret
$tenantId = “<tenant-id>”
$clientId = “<client-id>”
$clientSecret = “<client-secret>”
# Set the Azure subscription ID, resource group name, and location
$subscriptionId = “<subscription-id>”
$resourceGroupName = “<resource-group-name>”
$location = “<location>”
# Obtain a bearer token using Azure AD client credentials flow
$tokenEndpoint = “https://login.microsoftonline.com/$tenantId/oauth2/v2.0/token”
$body = @{
    grant_type    = “client_credentials”
    client_id     = $clientId
    client_secret = $clientSecret
    scope         = “https://management.azure.com/.default”
}
$response = Invoke-RestMethod -Method Post -Uri $tokenEndpoint -Body $body
$accessToken = $response.access_token
# Create the resource group using Azure REST API
$resourceGroupEndpoint = “https://management.azure.com/subscriptions/$subscriptionId/resourcegroups/$resourceGroupName”
$resourceGroupEndpoint = $resourceGroupEndpoint + “?” + “api-version=2021-04-01”
$headers = @{
    Authorization = “Bearer $accessToken”
    “Content-Type” = “application/json”
}
$body = @{
    location = $location
} | ConvertTo-Json
$response = Invoke-RestMethod -Method Put -Uri $resourceGroupEndpoint -Headers $headers -Body $body
Write-Host “Responce:”
Write-Host $response