1

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.

Blue Clouds
  • 7,295
  • 4
  • 71
  • 112

1 Answers1

1

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
Blue Clouds
  • 7,295
  • 4
  • 71
  • 112
  • https://stackoverflow.com/questions/69878737/how-to-invoke-ps-script-stored-in-file-share-with-azure-run-book Can you check that? – UDIT JOSHI Nov 08 '21 at 17:30
  • To note from [the documentation](https://learn.microsoft.com/en-us/powershell/module/az.compute/invoke-azvmruncommand?view=azps-8.2.0): "-ScriptPath Path of the script to be executed. When this value is given, the given script will override the default script of the command. Path should point to a file from a local file system. The command will load it and send it for execution.". *"local file system"* – foxx1337 Sep 06 '22 at 00:31