0
votes

Given below command giving the output as object

$output = (Invoke-AzVMRunCommand -ResourceGroupName $rgname -Name $vmname -CommandId 'RunPowerShellScript' -ScriptPath authoring.ps1).value

 Output

Mode             : Process
ContextDirectory : 
ContextFile      : 
CacheDirectory   : 
CacheFile        : 
Settings         : {}

Code          : ComponentStatus/StdOut/succeeded
Level         : Info
DisplayStatus : Provisioning succeeded
Message       : @{cluster_name=test01; status=green; timed_out=False; number_of_nodes=3; 
              number_of_data_nodes=3; 
              active_primary_shards=25; active_shards=50; relocating_shards=0; 
              initializing_shards=0; 
              unassigned_shards=0; delayed_unassigned_shards=0; number_of_pending_tasks=0; 
              number_of_in_flight_fetch=0; task_max_waiting_in_queue_millis=0; 
              active_shards_percent_as_number=100.0}
 Time          : 
 Code          : ComponentStatus/StdErr/succeeded
 Level         : Info
 DisplayStatus : Provisioning succeeded

How to get the Message ==> number_of_nodes = 3 and number_of_data_nodes=3 in some variable so based on this value I need to perform some action. Thanks

1
What does $output.Message.GetType() reveal?Theo
$output.Message.number_of_nodes Should give you what you needOtter
@TristanOtterpohl Its not working only working $output.Message and giving the specific data but inside the Message there is multiple key pair valueSudhir Goswami
$Output = gc E:\Result.txt foreach($temp in $Output) { if($temp -match "node") { [int]$number_of_nodes=(($temp -split ";")[3]-split "=")[1] [int]$number_of_data_nodes=(($temp -split ";")[4]-split "=")[1] } } $number_of_nodes $number_of_date_nodes ''' This code is working when I am copying this output in some text file. but when I am directly work with $output then its not workingSudhir Goswami
@Theo , IsPublic = True , IsSerial = True , Name = String and BaseType = System.ObjectSudhir Goswami

1 Answers

1
votes

Ok, if as you tested $Output.Message is a multiline string like

@{cluster_name=test01; status=green; timed_out=False; number_of_nodes=3; 
              number_of_data_nodes=3; 
              active_primary_shards=25; active_shards=50; relocating_shards=0; 
              initializing_shards=0; 
              unassigned_shards=0; delayed_unassigned_shards=0; number_of_pending_tasks=0; 
              number_of_in_flight_fetch=0; task_max_waiting_in_queue_millis=0; 
              active_shards_percent_as_number=100.0}

you can convert this into a Hashtable like this:

$data = $Output.Message.Trim("@{}") -replace ';', [environment]::NewLine | ConvertFrom-StringData

If you print that out to screen with $data | Format-Table -AutoSize it looks like this:

Name                             Value 
----                             ----- 
number_of_nodes                  3     
task_max_waiting_in_queue_millis 0     
number_of_data_nodes             3     
status                           green 
initializing_shards              0     
active_shards_percent_as_number  100.0 
cluster_name                     test01
delayed_unassigned_shards        0     
active_shards                    50    
unassigned_shards                0     
active_primary_shards            25    
number_of_in_flight_fetch        0     
timed_out                        False 
relocating_shards                0     
number_of_pending_tasks          0 

With this Hashtable format, it is easy to get the values of the different keys. For instance:

$data.number_of_nodes            # --> 3
$data.number_of_data_nodes       # --> 3
$data.cluster_name               # --> test01