Reputation: 304
I have searched around to execute commands on my Linux VM via Powershell on my Windows laptop. I found that there is Azure CLI command az vm run-command invoke
to invoke a command.
All I found in powershell is Invoke-AzureRmVMRunCommand
which takes a "script file path" (that means I have to find a way to upload the script file to VM first! what a hassle!) instead of putting direct commands.
Is there a az vm run-command invoke
equivalent command in Powershell?
Thank you.
Upvotes: 0
Views: 3123
Reputation: 304
As the result, I misunderstood the command Invoke-AzureRmVMRunCommand . In the command Invoke-AzureRmVMRunCommand the parameter ScriptPath is actually a local file -- not the file uploaded on VM.
So that what I can do is to put all my bash scripts in the file system_setup.sh at the same directory as my Powershell and call the following command :
Invoke-AzureRmVMRunCommand -ResourceGroupName $ResourceGroupName -Name $VMName -CommandId 'RunShellScript' -ScriptPath '.\system_setup.sh'
Then Azure will upload and execute this shell script on the remote Linux VM as user root (not the ssh user set for VM) under the directory /var/lib/waagent/run-command/download/0
Upvotes: 1
Reputation: 1812
Look at the PowerShell section of this Microsoft documentation page: https://learn.microsoft.com/en-us/azure/virtual-machines/windows/run-command#powershell
Here is a simple one I ran:
$resource_name="myVM"
$resource_group="myRG"
az vm run-command invoke -g "${resource_group}" -n "${resource_name}" --command-id
RunPowerShellScript' --scripts "Write-Output 'testing russ'"
The output was:
{ "additionalProperties": {}, "endTime": "2018-07-25T00:09:42.479247+00:00", error": null, "name": "2a535b1a-e561-4a62-af43-c3c5c279bbcd", "output": [ { code": "ComponentStatus/StdOut/succeeded", "displayStatus": "Provisioning ucceeded", level": "Info", "message": "testing russ" }, { "code": ComponentStatus/StdErr/succeeded", "displayStatus": "Provisioning succeeded", level": "Info", "message": "" } ], "startTime": "2018-07-5T00:09:09.605015+00:00", "status": "Succeeded"}
Hope this helps.
Upvotes: 0