0

I am trying to run the below powershell script

The below script is being used to list local users in a VM


$subscriptions=Get-AzSubscription -SubscriptionId "##################"

foreach ($subscription in $subscriptions){

Select-AzSubscription -SubscriptionId $subscription.SubscriptionId

$instances=Get-AzVM -Status | Select-Object Name,PowerState,ResourceGroupName ,@{l='osType';e={$\_.StorageProfile.osDisk.osType}}

foreach ($instance in $instances){

    Write-Host $instance.Name.Name

    Write-Host $instance.osType

    if($instance.osType -eq "Windows"){

        Write-Host "Windows server"

        if($instance.PowerState -eq "VM running"){
$users=Invoke-AzVMRunCommand -ResourceGroupName $instance.ResourceGroupName -Name $instance.Name -CommandId 'RunPowerShellScript' -ScriptPath 'C:\Users\tushar.raichand\Desktop\sample.ps1'

Write-Host $users

Write-Host "####################################################"

foreach($user in $users){

Write-Host $user

}

        }

    }

    else{

        if($instance.PowerState -eq "running"){
Write-Host "Linux server"

        }



    }

}
}

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

Where as when i just run this command in console


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

I am getting the users list.

Tushar Raichand
  • 91
  • 3
  • 12
  • this is the third question about the same thing, please explain how it is not – 4c74356b41 Apr 02 '19 at 09:59
  • 1st post i have changed it back to original question, 2nd post i have deleted it. and this is the 3rd post, so it is not duplicate now. – Tushar Raichand Apr 02 '19 at 10:02
  • `Write-Host` (which is only designed for writing to the _display_ and which cannot output _data_), performs space-separated, single-line output formatting based on simple `.ToString()` calls, which with complex objects typically results in unhelpful representations. For richly formatted display-only output, use `Out-Host` instead; for data, use `Write-Output` or, better yet, PowerShell's _implicit_ output feature - see [this answer](https://stackoverflow.com/a/58240860/45375). – mklement0 Feb 23 '21 at 14:35

1 Answers1

4

Well, I can reproduce your issue. The issue was caused by Write-Host $users, you need to change it to Write-Output $users.

enter image description here

Sample:

$users=Invoke-AzVMRunCommand -ResourceGroupName $instance.ResourceGroupName -Name $instance.Name -CommandId 'RunPowerShellScript' -ScriptPath 'C:\Users\joyw\Desktop\sample.ps1' 
Write-Output $users

enter image description here

Besides, your script has some small mistakes, e={$\_.StorageProfile.osDisk.osType} should be e={$_.StorageProfile.osDisk.osType}. Write-Host $instance.Name.Name should be Write-Host $instance.Name, it is enough.

Community
  • 1
  • 1
Joy Wang
  • 39,905
  • 3
  • 30
  • 54
  • Thanks for your reply... I am getting the Values now with "$users=$output.value[0].message" but i am not able to access the values inside this table. Any idea how that can be done? – Tushar Raichand Apr 03 '19 at 05:40
  • @TusharRaichand Try `$users.Value.Message`, it will be like https://i.stack.imgur.com/sMvbZ.png. – Joy Wang Apr 03 '19 at 05:46
  • Yes that is working but if i want to access the values inside this table, how can i do that. For example i want to access Name column and print their values. – Tushar Raichand Apr 03 '19 at 05:49
  • @TusharRaichand Seems it is a string, my workaround is to store it as a file and read it. – Joy Wang Apr 03 '19 at 06:56
  • I have raised a question for the same https://stackoverflow.com/questions/55489841/how-to-read-text-in-txt-file-and-each-of-its-value-in-powershell. – Tushar Raichand Apr 03 '19 at 07:58