Search code examples
powershelldatagridpowercli

VMware Cluster > Host > VM datagrid view


I am looking to create an easy powershell script which will get all clusters in a vcenter and for each one, get a list of hosts in that cluster, and then list all VMs in that cluster. I can create the datagrid easy enough, though am unsure how to get it to change the row sizes.

Currently I have this but I don't know if it is actually working:

$Grid.Columns["Hosts"].DefaultCellStyle.WrapMode = [System.Windows.Forms.DataGridViewTriState]::True

Currently when I run it, I get the grid with my 3 columns (Cluster, Hosts, VMs). However in the Hosts and VMs it displays System.Object[] instead of listing all hosts and VMs.

Lastly, would it be possible if say there are 5 hosts in the list, the row would grow to be 5 lines wide. If there are say 14 VMs in the same cluster, could I have it list VMs say 5, then 5, then 4 (so almost have columns in the row)?

Here is sorta what I have for the function to get the cluster, hosts, and VMs. Perhaps my issue is in there as to why it shows System.Object:

Function Get-Info
{
$clusters = Get-Cluster

Foreach ($cluster in $clusters)
   {
    $hosts = get-cluster -name $cluster | get-vmhost | select Name
    $vms = get-cluster -name $cluster | get-vm | select Name 

$Grid.Rows.Add($cluster,$hosts,$vms)
    }
}

Solution

  • Doing some digging after stepping away from it, I was able modify my code to the one below which will take multiple values and join them with a semi-colon. This allowed me to dump them into an Excel sheet and then I could just do a Find/replace on the semi-colon to seperate out values.

    In addition, I built an array to add in the values I want and pipe that into a CSV file.

    $array = @()
    
    $clusters = Get-Cluster
    
    Foreach ($cluster in $clusters)
    {
    
     $hosts = get-cluster -name $cluster | get-vmhost
     $gethosts =  ($hosts.Name | % { (get-vmhost $_).Name;}) -join ';';
     $vms = get-cluster -name $cluster | get-vm
     $getvms =  ($vms.Name | % { (get-vm $_).Name;}) -join ';';
    
    
        $obj = New-Object PSObject
    
        $obj | Add-Member -MemberType NoteProperty -Name "Cluster" -Value $cluster
    
        $obj | Add-Member -MemberType NoteProperty -Name "Hosts" -Value $gethosts
    
        $obj | Add-Member -MemberType NoteProperty -Name "VMs" -Value $getvms
    
        $array += $obj
    }
    
    
    $array |  select Cluster, Hosts, VMs| export-csv "c:\temp\test\clusterinfo.csv" -NoTypeInformation -encoding ASCII -force