Search
Join the Technical Preview Program
See how NVMe-oF removes iSCSI
bottlenecks in your HCI
The Best Hyperconverged
Infrastructure
(HCI) for Enterprise
ROBO, SMB & Edge
The Best Virtual SAN
for Enterprise ROBO, SMB & Edge

How to Use Azure REST API with PowerShell

  • January 5, 2021
  • 5 min read
IT Production Manager and Microsoft MVP. Nicolas specializes in Cloud and Datacenter Management. Focused on the Microsoft ecosystem, he provides expertise in Azure, Windows Server, and IT infrastructure. As an MVP, Nicolas delivers technical insights into optimizing enterprise cloud strategy and production-grade datacenter operations.
IT Production Manager and Microsoft MVP. Nicolas specializes in Cloud and Datacenter Management. Focused on the Microsoft ecosystem, he provides expertise in Azure, Windows Server, and IT infrastructure. As an MVP, Nicolas delivers technical insights into optimizing enterprise cloud strategy and production-grade datacenter operations.

Red Shirt Dev Tour 2018 à Paris - Hands On Labs avec Graph API - Cellenza Blog

Sometimes you might want to get some information about Azure virtual machines. In this article, we will explore Azure Graph API to extract information about your virtual machines with a very simple PowerShell script.

More information about Graph API : https://docs.microsoft.com/en-us/azure/governance/resource-graph/overview

Open the Azure Portal, go to App Registrations

 App Registrations

Click New Registration to create a new app

New Registration

Enter a friendly name to your application. Regarding the URI, it is optional, so you can add https:///localhost

Register

Once it is created, you will see the information on the home page. We will need to copy/paste these information later.

Home page

Go to Certificates & secrets to create a Client Secret that will be used to authenticate to the Azure REST API calls.

Certificates & secrets

Click on New Client secret to generate the secret.

New Client secret

Add a description and select if you want expiration or not

Add a description

Be careful, you have to save the key somewhere as it will not be accessible after.

Save the key somewhere

Now go to Subscription and the IAM blade. Click Add / Add role assignment

Add role assignment

Search the App created previously and select the Reader role for your App.

Reader role

Copy/paste the subscription ID as it will be required later.

Subscription ID

Everything is ok, so now we can use the following PowerShell script in order to query the Azure subscription.

$SubscriptionId = "zzzzzzzz-zzzz-zzzz-zzzzzzzz"

$TenantId = "yyyyyyyy-yyyy-yyyy-yyyy-yyyyyyyyyy"

$ClientId = "xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxx"

$ClientSecret = "<client_secret>"




$Resource = "https://management.core.windows.net"

$RequestAccessTokenUri = "https://login.microsoftonline.com/$TenantId/oauth2/token"




$body = "grant_type=client_credentials&client_id=$ClientId&client_secret=$ClientSecret&resource=$Resource"




# Get Access Token

$AccessToken = Invoke-RestMethod -Method Post -Uri $RequestAccessTokenUri -Body $body -ContentType 'application/x-www-form-urlencoded'




# Get Azure Virtual Machines

$VM = "https://management.azure.com/subscriptions/$SubscriptionId/providers/Microsoft.Compute/virtualMachines?api-version=2020-12-01"




# Format Header

$Headers = @{}

$Headers.Add("Authorization","$($AccessToken.token_type) "+ " " + "$($AccessToken.access_token)")




# Invoke REST API

$VMs = Invoke-RestMethod -Method Get -Uri $VM -Headers $Headers

$VMs.value | ForEach-Object {

Write-Output $_.Name

}

This script returns information about Virtual Machines.

Information about Virtual Machines

Of course, you can get information about all the Azure resources, please check the following documentation: https://docs.microsoft.com/en-us/rest/api/resources/

Found Nicolas’s article helpful? Looking for a reliable, high-performance, and cost-effective shared storage solution for your production cluster?
Dmytro Malynka
Dmytro Malynka StarWind Virtual SAN Product Manager
We’ve got you covered! StarWind Virtual SAN (VSAN) is specifically designed to provide highly-available shared storage for Hyper-V, vSphere, and KVM clusters. With StarWind VSAN, simplicity is key: utilize the local disks of your hypervisor hosts and create shared HA storage for your VMs. Interested in learning more? Book a short StarWind VSAN demo now and see it in action!