Invoke-AzureRmVMRunCommand - Azure Automation - Path of the file when I try to run from azure automation. RRS feed

  • Question

  • Hello Team,

    I am trying to trigger a script that is placed inside the virtual machine from the azure automation account. When I try the command 

    Invoke-AzureRmVMRunCommand -ResourceGroupName $rgname -Name $vmname -CommandId 'RunPowerShellScript' -ScriptPath 'c:\test.ps1'

    From my powershell ISE, it works fine. However when I run it from azure-automation runbook it fails saying Cannot find the file.

    Friday, August 23, 2019 3:12 PM

All replies

  • Hi Ravikiran1,

    Have your Azure Automation runbook something like shown below. It should 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 = "c:\test.ps1"

    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 and "vvvvvvvvvvvvvv" with your VM name

    Hope this helps!! Cheers!!
    Friday, August 23, 2019 4:30 PM