Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get output stream for Invoke-AzureRmVMRunCommand command?

I am trying to run the below command

Invoke-AzVMRunCommand -ResourceGroupName $instance.ResourceGroupName -Name $instance.Name -CommandId 'RunPowerShellScript' -ScriptPath 'C:\Users\tushar.raichand\Desktop\sample.ps1'

Sample.ps1 is as below

$output = Get-LocalUser
Write-Output $output
$output

The output i am getting for Invoke-AzVMRunCommand is

Microsoft.Azure.Commands.Compute.Automation.Models.PSRunCommandResult
like image 380
Tushar Raichand Avatar asked Feb 26 '26 14:02

Tushar Raichand


1 Answers

First, make sure you have enough permission to show the details of a command, see Limiting access to Run Command:

Listing the run commands or showing the details of a command require the Microsoft.Compute/locations/runCommands/read permission, which the built-in Reader role and higher have.

Besides, the command Invoke-AzureRmVMRunCommand belongs to the AzureRM powershell module which has been deprecated, you may need to upgrade it to the new Az module, refer to this link to upgrade.

I test the script with the new Az command Invoke-AzVMRunCommand, it works fine.

Invoke-AzVMRunCommand -ResourceGroupName joywebapp -Name joyVM -CommandId 'RunPowerShellScript' -ScriptPath 'C:\Users\joyw\Desktop\sample.ps1'

sample.ps1:

$output = Get-LocalUser
Write-Output $output

Result:

enter image description here

like image 96
Joy Wang Avatar answered Feb 28 '26 06:02

Joy Wang