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:

Posted on Leave a comment

Authenticate to Azure management API using az cli

A quick an easy way to authenticate on Azure management API is to use az cli get-access-token command and use this as a Bearer token on your API calls.

Given that you want to access https://management.azure.com/subscriptions/api you can use:

$token =  az account get-access-token | ConvertFrom-Json

You can then get the access token with $token.accesstoken

Finally you can use this token as a Bearer token on your API calls

$headers = @{ Authorization = "Bearer $token.accesstoken" }
Posted on Leave a comment

Powershell -Contains does not evaluate string with Get-Content

When using powershell contains method you should be aware of the type of the object that is used.

Lets examine the below string which was used with contains on a powershell script.

Processed 384 pages for database ‘restore’, file ‘restore’ on file 1.
Processed 2 pages for database ‘restore’, file ‘restore_log’ on file 1.
RESTORE DATABASE successfully processed 386 pages in 0.004 seconds (752.929 MB/sec).

The string was stored on $result variable and I wanted to capture if a specific string was included in the variable. Although the string is included in the variable the result was false.

After investigating I was able to figure that the object type of the $result variable was an object and Contains could not evaluate successfully

In this situation you would have to cast the object on a string data type to evaluate correctly.

For example when using Get-Content you could cast to a string like below.

[string]$result = Get-Content C:\Users\galexiou\Desktop\2.txt

Then the object type will be of type string

The evaluation will work as expected.