Control Azure VM with PowerShell and Azure REST API

Today I want to show you how to control an Azure VM with PowerShell and Azure REST API. So to Start, Stop or Restart an Azure VM.

Also, to do this with as least permission as possible, with an Azure App Reg and Custom Role.

As always, a good start is the LEARN Platform from Microsoft, so let’s read here for some more Details: Virtual Machines – REST API (Azure Compute) | Microsoft Learn

What we need to control an Azure VM

  • Azure App Reg
  • Custom Azure Role
  • VM’s

Azure App Registration

So, we do not get into Details here. Creating an Azure Application Registration is a straight Process. See some Details here: Use Microsoft Graph API with PowerShell – Part 1 – TechGuy

The result is an App Reg with Name, Client ID, and Secret

Custom Role

So, as we have an Azure App Reg, we need to add some permission to it. The easiest way would be to grant a “Contributor” Role to the App for all Subscriptions. But as Security is essential, let’s try a different way and create a Custom Role with only needed Permissions.

So Navigate to your Subscription and click “Access control (IAM).”

Next, click “Add” and “Add custom role.”

Enter a Name, and NEXT

On the Permissions Tab, add the following and NEXT

On the “Assignable scopes” Tab, add all Subscriptions; this Role should be available in, and NEXT and CREATEth

So, now we have a Custom Role, then please assign this new Role to the created Azure App Reg

No, we prepared all the Security related Stuff. Let’s see the Script

The Script

First, let’s see some variables in the Script; the whole Script is available on GitHub below.

Enter here your Azure App Reg Details

Now we need some Details for your Azure VM and the Action you want to trigger.

Azure Rest API Authentication is done here. Please do not change anything

Here we trigger your Action and what for a “Succeeded.”

SELF SERVICE!!!

Control Azure VM as Self Service with au2mator

With au2mator Self Service Portal, you can create a Service and delegate the task to control Azure VM.

Some of the Use cases❗
✔️Delegate the Control of an Azure VM
✔️Contrl your Azure VM outside Azure Portal.
✔️Mass Start/Stop your Azure VMs
✔️Many more…

More Details: www.au2mator.com

GitHub Repo

Here is the Azure REST API GitHub Repo with other examples: Seidlm/Microsoft-Azure Examples (github.com)

Michael Seidl aka Techguy
au2mate everything

1 thought on “Control Azure VM with PowerShell and Azure REST API”

  1. Pingback: Microsoft Cloud ve Datacenter Management Ocak 2023 Bülten – Sertaç Topal

Leave a Comment

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

*