Home > Powershell, VMWare > Powershell – Converting multiple return values to single strings for reports

Powershell – Converting multiple return values to single strings for reports

Sometimes, you’re writing a little Powercli / Powershell code to generate a report and the repotr you generate retuns multiple values for some lines.

for example, you want a report showing each ESX host in your environment’s mapped VLANs and Datastores – or you’d like to interrogate some servers and return their DNSServers – as single objects.

[string]::Join is your friend here.

for example:

Get-VirtualPortGroup -VMHost $vmhost | %{$_.VlanId}
2598
2599
553
552
638
637
104
119
2470
2460
2951
2465
19
17
50
633
634
635
636

Hmm – this has returned a nice list – but I don;t want a new line for each VLAN.

so let’s join that into a comma sepated string?

[string]::Join(',',(Get-VirtualPortGroup -VMHost $vmhost | %{$_.VlanId}))
2598,2599,553,552,638,637,104,119,2470,2460,2951,2465,19,17,50,633,634,635,636

Ahh – much better!

so now I can generate my original report to find VLANs and Datastores mapped to hosts in my cluster

get-cluster $cluster | Get-VMHost | Select Name,
    @{N="VLAN";E={[string]::Join(',',(Get-VirtualPortGroup -VMHost $_ | %{$_.VlanId}))}},
    @{N="Datastore";E={[string]::Join(',',(Get-Datastore -VMHost $_ | %{$_.Name}))}}
Name                                              VLAN                                              Datastore
----                                              ----                                              ---------
LABESX8.get-virtual.info                         3410,3415,3430,3435,3425,3420,0,0,2640           LABSTG112L055,LABSTG112L062,LABSTG112L012..
labesx17.get-virtual.info                         3410,3415,2640,3425,3435,3430,3420,0,0,0         LABSTG112L055,LABSTG112L062,LABSTG112L099..
labesx18.get-virtual.info                         3410,3415,3430,3435,3425,3420,0,0,2640           LABSTG112L055,LABSTG112L062,LABSTG112L099..
labesx21.get-virtual.info                         3410,3415,2640,3435,3430,3425,3420,0,0           LABSTG112L055,LABSTG919119,LABSTGHA919L116_FI,..
labesx40.get-virtual.info                         3410,3415,2640,3435,3430,3425,3420,0,0           LABSTG112L055,LABSTG112L062,LABSTG112L099..
labesx25.get-virtual.info                         3410,3415,2640,3435,3430,3420,3425,0,0           LABSTG112L055,LABSTG919119,LABSTGHA919L116_FI,..

the magic lies here:

[string]::Join(',',(

What this is saying is joing the response from the following queery – into 1 string . .seperated by ‘,’

job done – pretty reports here we come!

Categories: Powershell, VMWare Tags:
  1. No comments yet.
  1. No trackbacks yet.
You must be logged in to post a comment.