Hi @b-dub ,
To connect remotely to an Azure VM and to execute a script with the help of Azure Automation, you can follow one of the below 2 approaches:
- Create an Azure storage account, container and upload a blob i.e., script and then have below code in Azure Automation PowerShell runbook.
Windows:
$connectionName = "AzureRunAsConnection"
$servicePrincipalConnection = Get-AutomationConnection -Name $connectionName
$ConnectToAzAccount = Add-AzAccount -ServicePrincipal -TenantId $servicePrincipalConnection.TenantId -ApplicationId $servicePrincipalConnection.ApplicationId -CertificateThumbprint $servicePrincipalConnection.CertificateThumbprint
$StorageAccountName = "xxxxxxxxxxxxx"
$StorageAccountKey = "xxxxxxxxxxxxxx=="
$ContainerName = "xxxxxxxxxxxxxxx"
$BlobName_Windows = "samplescript.ps1"
$RG_VM = "xxxxxxxxxxxxxxxxxx"
$VM_Name_Windows = "xxxxxxxxx"
$InvokeCmd_Id_Windows = "RunPowerShellScript"
$AzStorage = New-AzStorageContext -StorageAccountName $StorageAccountName -StorageAccountKey $StorageAccountKey
$AzStorageContext = $AzStorage.Context
$GetBlobContent_Windows = Get-AzStorageBlobContent -Container $ContainerName -Blob $BlobName_Windows -Destination ($Env:temp+"/samplescript.ps1") -Context $AzStorageContext -Force
$InvokeRunCmdOutput_Windows = Invoke-AzVMRunCommand -ResourceGroupName $RG_VM -VMName $VM_Name_Windows -CommandId $InvokeCmd_Id_Windows -ScriptPath ($Env:temp+"/samplescript.ps1")
$SampleScript_Output_Windows = $InvokeRunCmdOutput_Windows.Value[0].Message
Write-Output $SampleScript_Output_Windows
(or)
Linux:
$connectionName = "AzureRunAsConnection"
$servicePrincipalConnection = Get-AutomationConnection -Name $connectionName
$ConnectToAzAccount = Add-AzAccount -ServicePrincipal -TenantId $servicePrincipalConnection.TenantId -ApplicationId $servicePrincipalConnection.ApplicationId -CertificateThumbprint $servicePrincipalConnection.CertificateThumbprint
$StorageAccountName = "xxxxxxxxxxxxx"
$StorageAccountKey = "xxxxxxxxxxxxxx=="
$ContainerName = "xxxxxxxxxxxxxxx"
$BlobName_Linux = "samplescript.sh"
$RG_VM = "xxxxxxxxxxxxxxxxxx"
$VM_Name_Linux = "xxxxxxxxx"
$InvokeCmd_Id_Linux = "RunShellScript"
$AzStorage = New-AzStorageContext -StorageAccountName $StorageAccountName -StorageAccountKey $StorageAccountKey
$AzStorageContext = $AzStorage.Context
$GetBlobContent_Linux = Get-AzStorageBlobContent -Container $ContainerName -Blob $BlobName_Linux -Destination ($Env:temp+"/samplescript.sh") -Context $AzStorageContext -Force
$InvokeRunCmdOutput_Linux = Invoke-AzVMRunCommand -ResourceGroupName $RG_VM -VMName $VM_Name_Linux -CommandId $InvokeCmd_Id_Linux -ScriptPath ($Env:temp+"/samplescript.sh")
$SampleScript_Output_Linux = $InvokeRunCmdOutput_Linux.Value[0].Message
Write-Output $SampleScript_Output_Linux
- Save the script in a folder in your Azure VM and then have below code in Azure Automation PowerShell runbook.
Windows:
$ServicePrincipalConnection = Get-AutomationConnection -Name 'AzureRunAsConnection'
Add-AzAccount -ServicePrincipal -TenantId $ServicePrincipalConnection.TenantId -ApplicationId $ServicePrincipalConnection.ApplicationId -CertificateThumbprint $ServicePrincipalConnection.CertificateThumbprint
$rgname ="rrrrrrrrrrrrrr"
$vmname ="vvvvvvvvvvvvvv"
$ScriptToRun = "samplescript.ps1"
Out-File -InputObject $ScriptToRun -FilePath ScriptToRun.ps1
Invoke-AzVMRunCommand -ResourceGroupName $rgname -Name $vmname -CommandId 'RunPowerShellScript' -ScriptPath ScriptToRun.ps1
Remove-Item -Path ScriptToRun.ps1
(or)
Linux:
$ServicePrincipalConnection = Get-AutomationConnection -Name 'AzureRunAsConnection'
Add-AzAccount -ServicePrincipal -TenantId $ServicePrincipalConnection.TenantId -ApplicationId $ServicePrincipalConnection.ApplicationId -CertificateThumbprint $ServicePrincipalConnection.CertificateThumbprint
$rgname ="rrrrrrrrrrrrrr"
$vmname ="vvvvvvvvvvvvvv"
$ScriptToRun = "\home\xxxx\samplescript.sh"
Out-File -InputObject $ScriptToRun -FilePath ScriptToRun.sh
Invoke-AzVMRunCommand -ResourceGroupName $rgname -Name $vmname -CommandId 'RunShellScript' -ScriptPath ScriptToRun.sh
Remove-Item -Path ScriptToRun.sh
Source of the above information: https://learn.microsoft.com/en-us/answers/questions/526820/index.html
So, yes, you can either use any solution to automatically start or stop the VM or else have another Azure Automation runbook to start or stop the VM using Start-AzVm / Stop-AzVM Az PS cmdlets and then use one of the above mentioned runbook to connect to the VM remotely and execute a script. For scheduling purpose, you may leverage Azure Automation schedules.