Mund
Mund

Reputation: 157

Azure runbook - run Powershell on remote VM

Azure runbook.

The question:

How to run/call powershell scripts on remote Azure VM via runbook? The script is placed on a remote VM.

There is no Azure AD, powershell has Az module installed.

Thank you.

Upvotes: 0

Views: 3784

Answers (1)

KrishnaG
KrishnaG

Reputation: 3484

Have your Azure Automation runbook something like shown below. It will accomplish your requirement.

$ServicePrincipalConnection = Get-AutomationConnection -Name 'AzureRunAsConnection'
Add-AzAccount -ServicePrincipal -TenantId $ServicePrincipalConnection.TenantId -ApplicationId $ServicePrincipalConnection.ApplicationId -CertificateThumbprint $ServicePrincipalConnection.CertificateThumbprint
$rgname ="rrrrrrrrrrrrrr"
$vmname ="vvvvvvvvvvvvvv"
$ScriptToRun = "ssssssssssssss"
Out-File -InputObject $ScriptToRun -FilePath ScriptToRun.ps1 
Invoke-AzVMRunCommand -ResourceGroupName $rgname -Name $vmname -CommandId 'RunPowerShellScript' -ScriptPath ScriptToRun.ps1
Remove-Item -Path ScriptToRun.ps1

Note: Before you run your runbook, make sure you update "rrrrrrrrrrrrrr" with your resource group name, "vvvvvvvvvvvvvv" with your VM name and "ssssssssssssss" with the path of the script along with script name

For reference, you may refer the source from here.

Hope this helps!! Cheers!!

Upvotes: 3

Related Questions