Running Programs in Azure VM without connecting

b-dub 1 Reputation point
2021-08-17T15:37:13.017+00:00

I am using RPA tool UiPath on an Azure Virtual Machine with the goal to run a scheduled script everyday. I have been coding the script by connecting to the VM with Remote Desktop Connection. Will it be possible to run this workflow on the Azure VM without connecting to it via Remote Desktop Connection? Essentially, I want the virtual machine to start everyday at a scheduled time and run the program even if no computer is connected.

Azure Virtual Machines
Azure Virtual Machines
An Azure service that is used to provision Windows and Linux virtual machines.
9,013 questions
Azure Automation
Azure Automation
An Azure service that is used to automate, configure, and install updates across hybrid environments.
1,366 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. George Raduta 1 Reputation point
    2021-08-17T20:02:49.267+00:00

    Hello @b-dub

    You can use Start / Stop for shutting down and turning back on the VM at specific time. See https://learn.microsoft.com/en-us/azure/automation/automation-solution-vm-management

    Once the VM boots you can use Task Scheduler to run the script. See https://learn.microsoft.com/en-us/windows/win32/taskschd/task-scheduler-start-page


  2. tbgangav-MSFT 10,426 Reputation points Moderator
    2021-09-02T08:29:08.29+00:00

    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.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.