Azure Devops pipeline to connect with Azure VM without ARM service connection

Dileep Veldi
3 min readMar 22, 2021

--

Connecting to Azure VM from Azure DevOps without service connection

Without having an established “Azure Resource Manager (ARM) service connection”, are you planning to run some commands on Azure virtual machine (VM)?

Prerequisites:
To complete this tutorial, you need the following
1) Windows Azure VM
2) Domain service account (must be administrators group member on Azure VM(s))
3) Hosted or Private Build agent
4) Azure release pipeline with a powershell inline task

Here i focussed on two scenarios, to interact with “multiple Azure VM(s)” without using Azure “ARM Service Connection”
a) A Pipeline task running on Azure Pipelines (Hosted) agent
b) A Pipeline task running on self-hosted (Private) agent using your service account

Hosted Agent:

In this scenario, the pipeline will be running with some default user account. So you will be creating a “session object” with in the task, using your service account listed in prerequisite 2, to interact with VM.

1) Create variables userNameSA, passwordSA (change variable type to secret) , vmNames

2) Add a PowerShell task

3) Add this script as inline script
Note
: Here i have 3 VM(s) listed in pipeline variable vmNames. Number of Session objects $s1, $s2… will depend on the number of Vm(s) you added.

# Write your PowerShell commands here.
Write-Host ‘ - - - - - - - - - - - - - - - - - - - - - - - - -’
Write-Host ‘ Stop and Start a task scheduled job on Azure VM’
Write-Host ‘ - - - - - - - - - - - - - - - - - - - - - - - - -’ $securePassword = ConvertTo-SecureString $(passwordSA) -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential ($(usernameSA),$securePassword)
$s1, $s2, $s3 = New-PSSession -ComputerName $(vmNames) -Credential $cred
$s = $s1, $s2, $s3
Write-Host 'Stopping task scheduled jobs on Azure VM'Invoke-Command -Session $s -ScriptBlock {(Stop-ScheduledTask -TaskName "<ScheduledTaskName1>"),(Stop-ScheduledTask -TaskName "<ScheduledTaskName2>") }Write-Host 'Starting task scheduled jobs on Azure VM'Invoke-Command -Session $s -ScriptBlock {(Start-ScheduledTask -TaskName "<ScheduledTaskName1>"),(Start-ScheduledTask -TaskName "<ScheduledTaskName2>") }

4) Now save and queue pipeline.

Private Agent:

In this scenario, the pipeline will be running on private (self-hosted) agent with your service account listed in prerequisite 2. So you don’t need to create a “session object” to interact with Azure VM(s).

1) Create one variable vmNames with comma seperated list of VM(s) with full domain name

2) Add a PowerShell task

3) Add this script as inline script

# Write your PowerShell commands here.
Write-Host ‘ - - - - - - - - - - - - - - - - - - - - - - - - -’
Write-Host ‘ Stop and Start a task scheduled job on Azure VM’
Write-Host ‘ - - - - - - - - - - -’ - - - - - - - - - - - - - -
Write-Host 'Stopping task scheduled jobs on Azure VM'
$(vmNames)|%{Stop-ScheduledTask <ScheduledTaskName1> -CimSession $_}
$(vmNames)|%{Stop-ScheduledTask <ScheduledTaskName2> -CimSession $_}
Write-Host 'Starting task scheduled jobs on Azure VM'
$(vmNames)|%{Start-ScheduledTask <ScheduledTaskName1> -CimSession $_}
$(vmNames)|%{Start-ScheduledTask <ScheduledTaskName2> -CimSession $_}

4) Now save and queue pipeline.

--

--

Dileep Veldi

2x Azure Certified | Experienced Cloud Solutions Developer and DevOps Engineer