Posted in

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
$body = @{
    grant_type    = “client_credentials”
    client_id     = $clientId
    client_secret = $clientSecret
    scope         = “https://management.azure.com/.default&#8221;
}
$response = Invoke-RestMethod -Method Post -Uri $tokenEndpoint -Body $body
$accessToken = $response.access_token
# Create the resource group using Azure REST API
$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

Microsoft Certified Trainer, Office 365, AWS, Azure and Cloud Expert-Architect. In the IT world for over than 20 years.

Apart from the main area of Microsoft Azure expert in the field of infrastructure servers Windows Server 2003-2019, Microsoft Active Directory, Hyper-V Private Cloud, IIS, System Center, SQL.

Private Cloud, System Center, Hyper-V, Open Stack Expert and all Microsoft products Expert. Linux Server administrator.

My Azure community projects:

https://mazeball.azurewebsites.net/
https://github.com/MariuszFerdyn?tab=repositories

More