2

I have an issue with the az vm run-command.

I have got it working inline as follows:

$resource_name="winsecagentdev2"
$resource_group="russellmccloydev"

and:

az vm run-command invoke `
-g "${resource_group}" `
-n "${resource_name}" `
--command-id 'RunPowerShellScript' `
--scripts "Write-Output 'testing russ'"

It works.

But when I try to use a .ps1 file:

az vm run-command invoke `
-g "${resource_group}" `
-n "${resource_name}" `
--command-id 'RunPowerShellScript' `
--scripts @{'hello.ps1'} `
--parameters @{ "test" = "russell" }

I get this issue:

Enter image description here

The documentation says this:

Enter image description here

So I am kind of stuck.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
RuSs
  • 1,725
  • 1
  • 29
  • 47

3 Answers3

3

You can try to put the file with the following format: --scripts "@hello.ps1"`

Screenshot

hello.ps1

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
axfd
  • 291
  • 1
  • 6
1

You need to put the file in your VM and use the format below:

--scripts "C:\test\hello.ps1"

result:

enter image description here

Joy Wang
  • 39,905
  • 3
  • 30
  • 54
  • ok but when I put my script on the destination machine at c:\getAccessToVM.ps1 if says file not found. It's actually looking on the caller machine for the file and not the destination. – RuSs Jul 25 '18 at 04:46
  • @RuSs It works fine on my side, could you provide the error message? Or try the path like I mentioned? ps: my vm is windows OS. – Joy Wang Jul 25 '18 at 05:11
  • Will do but can you share you hello.ps1 script? maybe ill start with that and work backwards. Mine is also a windows VM – RuSs Jul 25 '18 at 05:19
  • @RuSs Just `Write-Host hello` in my script, you could see the `"message":"hello"` in the output. – Joy Wang Jul 25 '18 at 05:23
  • ok.. and are you calling az vm run-command invoke from a remote machine like your laptop? – RuSs Jul 25 '18 at 05:24
  • @RuSs I called the command in my PC, not the VM, the script file is in the VM. – Joy Wang Jul 25 '18 at 05:26
  • yes very helpful. It's working now. but sometimes it takes a long time and is intermittent with regard to success rate. But all good, thanks – RuSs Jul 25 '18 at 05:39
1

To run a script from a remote machine to a Linux VM in Azure, use:

az vm run-command invoke myResourceGroup -n myVm --command-id RunShellScript --scripts "@quality-check.pl" 

I had copied the quality-check.pl script in my current location on the remote machine.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Jay J
  • 91
  • 8