Posted on 4 Comments

Start multiple VMs using parameters with Azure DevOps pipeline

In this article I will explain how one can start multiple VMs on Azure using a pipeline automation. The steps you need to follow:

First create a service connection with your subscription. You should navigate to service connections -> azure resource manager and then select service principal (automatic)

You will then have to select the scope* the subscription or resource group this service connection will access.

In my scenario I added a friendly name of the subscription as Azure Subscription Service Connection

trigger: none
pr: none
parameters:
– name: vms
type: object
default: ["ubuntu1","windows-1"]
pool:
vmImage: ubuntu-latest
jobs:
– job: startvmjob
displayName: Start VMs
steps:
– checkout: none
– ${{ each vm in parameters.vms }}:
– task: AzureCLI@2
displayName: starting vm ${{vm}}
inputs:
azureSubscription: 'Azure Subscription Service Connection'
scriptType: 'pscore'
scriptLocation: 'inlineScript'
inlineScript: |
$vmrequest = az vm list | ConvertFrom-Json | Where-Object {$_.Name -Match "${{vm}}"}
az vm start –resource-group $vmrequest.resourcegroup –name $vmrequest.name

When you run the pipeline you will need to provide the VM names. You can also run this pipeline on a schedule using a cron task. My vms are ubuntu1, windows-1

By running the pipeline

Then by navigating on Azure you can notice the VM as started.

Video tutorial on YouTube:

4 thoughts on “Start multiple VMs using parameters with Azure DevOps pipeline

  1. Is it possible to add windows update and then let the vm stop after the windows update is done

    1. Given that you know the timeframe of the updates you can use az vm restart in order to restart the vm and complete the updates. You can also use az vm stop in order to deallocate the vm. https://learn.microsoft.com/en-us/cli/azure/vm?view=azure-cli-latest#az-vm-restart . Another way you can do those would be directly from the vm configuration where you can specify the automatic updates and restart based on a timeframe.

      1. This is for an AVD and Citrix img. I need todo weekly windows updates and don’t want to start them. Then logon to them and do the windows update. I’m looking for a weekly automatic process.

        1. I see. You could perform those actions through the az cli and some extra powershell for the windows updates.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.