RSS

Tag Archives: hashtable

PS: One-line create a custom object list

The one-liner challenge for me today:

  • Get a list of random VMs from each ESXi host in a cluster
  • Send the host name, vm name and IP address to a CSV file

Normally, this is straight forward, but because we only need grab 2 VMs from each host, it is not possible to just  pipe the VM objects into a CSV file since the loop breaks between each host. The solution is to store all records into a array of custom objects and then pipe that into a CSV file at the end.

$ListOfVMs = @() ;
get-cluster | ? { $_.name -match "cluster1" } |
get-vmhost | % { $_ | get-vm | ? { $_.name -match "dev" } |
select -first 2 |
% { $t = @{} |
    select VMhost,Name,IPAddress ;
    $t.VMHost = $_.VMHost;
    $t.Name = $_.Name ;
    $t.IPAddress = $_.guest.IPAddress[0] ;
    $ListOfVMs += $t }
} ; $ListOfVMs | export-csv .\mylist.csv -NoTypeInformation

 

Advertisements
 
Leave a comment

Posted by on April 18, 2018 in powershell

 

Tags: , , ,

PS1: Transposing a vertical list of records

Honestly, I don’t really know what term to call this other than “a vertical list of records”. What I mean a set of records that are listed vertically instead of having its field values in columns. Below table is what it looks like:

host1:FIELD1=value1
host1:FIELD2=value2
host1:FIELD4=value3
host1:FIELD5=value4

host2:FIELD1=value1
host2:FIELD2=value2
host2:FIELD3=value3
host2:FIELD4=value4

This output comes out from a really old CLI which extracts the inventory of hosts. The problem with the CLI is that it can only extract one host at a time and it displays in this vertical list format. To extract 100 host, you need to call the CLI 100 times and dump the output into a file, which is what it ended up looking like in the above. Read the rest of this entry »

 
Leave a comment

Posted by on October 14, 2016 in powershell

 

Tags: