Create Azure Resource Group with PowerShell and MS Graph API

In this post, I want to show you how you create an Azure Resource Group with PowerShell using MS GRAPH API.

Some of the Use cases❗
✔️Delegate the Azure Resource Group creation
✔️Control your Azure Resource Group outside Azure Portal.
✔️Mass Creation Azure of Resource Groups
✔️Many more…

API Reference and Permissions

We used the following Docs to get this Script up and running

and configured the Azure App Registration as needed

To learn more from Microsoft GRAPH API, see my Blog Series:
Part 1 – Authentication and Azure App – Use Microsoft Graph API with PowerShell – Part 1 » TechGuy
Part 2 – Oauth2.0 – Use Microsoft Graph API with PowerShell – Part 2 » TechGuy
Part 3 – First Powershell Script to get a Teams Lis and Walkthrough – Use Microsoft Graph API with PowerShell – Part 3 » TechGuy
Part 4 – this one – Use Microsoft Graph API with PowerShell – Part 4 » TechGuy

The Script


$applicationId = 'your Application ID'
$tenantId = 'your Tenant ID'
$secret = 'your Secret'

$subscriptionId = 'your Subscription ID'


#VM Details
$RessourceGroupName = "RG_TEST_RessourceGroup"

#Location
$Location = "northeurope"


#API Version
$apiversion="2021-04-01"

#Microsoft Azure Rest API authentication
#https://docs.microsoft.com/en-us/rest/api/azure/


$param = @{
  Uri    = "https://login.microsoftonline.com/$tenantId/oauth2/token?api-version=$apiversion";
  Method = 'Post';
  Body   = @{ 
    grant_type    = 'client_credentials'; 
    resource      = 'https://management.core.windows.net/'; 
    client_id     = $applicationId; 
    client_secret = $secret
  }
}

$result = Invoke-RestMethod @param
$token = $result.access_token



$headers = @{
  "Authorization" = "Bearer $($token)"
  "Content-type"  = "application/json"
}

$URL = "https://management.azure.com/subscriptions/$subscriptionId/resourceGroups/$($RessourceGroupName)?api-version=$apiversion"

$bodyNewRessourceGroup = @"
    {
        "location": "$location"
    }
"@


Invoke-RestMethod -Method PUT -URI $URL -headers $headers -body $bodyNewRessourceGroup


### Get Status

do {
  $URLtoGet = "https://management.azure.com/subscriptions/$subscriptionId/resourceGroups/$($RessourceGroupName)?api-version=$apiversion"
  $Result = Invoke-RestMethod -Method GET -URI $URLtoGet -headers $headers
  $result.properties.provisioningState
  Start-Sleep -Seconds 5
} until ($result.properties.provisioningState -ne "Creating")


GitHub Repo

Here you can find the GitHub Repo with a lot of other examples: Seidlm/Microsoft-Azure: Azure Rest API Examples (github.com)

Delegate to create an Azure Resource Group as Self Service with au2mator

With au2mator Self Service Portal, you can create a Service and delegate the task to create an Azure Resource Group.

Some of the Use cases❗
✔️Delegate the Azure Resource Group creation
✔️Control your Azure Resource Group outside Azure Portal.
✔️Mass Creation Azure of Resource Groups
✔️Many more…

More Details: www.au2mator.com

Michael Seidl aka Techguy
au2mate everything

Leave a Comment

Your email address will not be published. Required fields are marked *

*