3

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
Tushar Raichand
  • 91
  • 3
  • 12

1 Answers1

1

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

Joy Wang
  • 39,905
  • 3
  • 30
  • 54
  • I have made some changes to original question please have a look. – Tushar Raichand Apr 02 '19 at 08:50
  • @TusharRaichand For the original question, could you get the details of the output successfully now? – Joy Wang Apr 02 '19 at 08:56
  • 1
    Yes i getting the result of Invoke-AzVMRunCommand command when i run it in console, but not when i am running it in a powershell script. As mentioned above. – Tushar Raichand Apr 02 '19 at 09:08
  • @TusharRaichand They are totally two different questions, my reply solved your original question. I think you need to modify your question back and accept it as the answer, then post a new question. Then I could look into it, stackoverflow avoid to ask several different questios in the same post. – Joy Wang Apr 02 '19 at 09:23
  • Okay let me close this question and open a new one. – Tushar Raichand Apr 02 '19 at 09:38
  • @TusharRaichand Add a Note in your new question like: This post is different from xxx post to let others know they are in different environments, then it will not be duplicate. – Joy Wang Apr 02 '19 at 09:44
  • 1
    https://stackoverflow.com/questions/55471733/invoke-azurermvmruncommand-command-displaying-wrong-output-when-run-in-powershel – Tushar Raichand Apr 02 '19 at 10:02
  • I have created a new post.. please help... Have a look at above URL – Tushar Raichand Apr 02 '19 at 10:03