Blue Clouds
Blue Clouds

Reputation: 8191

"Could not find file" Invoke-AzureRmVMRunCommand

Using Azure automation, powershell runbook, I am trying to execute a script inside the VM.

$ServicePrincipalConnection = Get-AutomationConnection -Name 'AzureRunAsConnection'

Add-AzAccount -ServicePrincipal -TenantId $ServicePrincipalConnection.TenantId -ApplicationId $ServicePrincipalConnection.ApplicationId -CertificateThumbprint $ServicePrincipalConnection.CertificateThumbprint

$rgname ="rg-name"

$vmname ="vmName"

$ScriptToRun = "c:\temp\shutdown.ps1" #file exists in the VM
    
Invoke-AzVMRunCommand -ResourceGroupName $rgname -Name $vmname -CommandId 'RunPowerShellScript' -ScriptPath $ScriptToRun

But I get this error

"Could not find file 'c:\temp\shutdown.ps1'."

I found a similar problem mentioned here but the answer is confusing

"I found out that the file in question needs to exist on the local machine--not the remote machine."

We are only talking about one VM here so not sure what it means by local machine vs remote.

Upvotes: 1

Views: 1638

Answers (1)

Blue Clouds
Blue Clouds

Reputation: 8191

We need to load the file into an object and then run it like this and in the end remove it.

Out-File -InputObject $ScriptToRun -FilePath ScriptToRun.ps1 

Invoke-AzVMRunCommand -ResourceGroupName $rgname -Name $vmname -CommandId 'RunPowerShellScript' -ScriptPath ScriptToRun.ps1

Remove-Item -Path ScriptToRun.ps1

Upvotes: 1

Related Questions