3 Replies Latest reply on Nov 28, 2012 4:49 PM by jaydo123

    help with simple script to list cluster and hosts in the cluster

    jaydo123 Enthusiast

      hi I need a  bit of help to create a script that runs against the VC and get the datacentrs and from that the clusters and host within each cluster then export to a CSV in the format

       

      • Cluster Name
      • ESX Host name #1
      • ESX Host name #2
      • Etc

       

      I have started a script but keep getting stuck here is what I  have so far it maybe totally wrong?   

       

      Connect-VIServer xxxxxx
      $datacenter = get-datacenter
      $Report = @()
      $clusters = Get-Cluster -Location $datacenter 
                      foreach {($cluster  in $Clusters)|sort Name | Get-VMHost | Sort Name)
      $ReportRow = "" | Select-Object Clusters, vmHosts
      $ReportRow.Cluster = Cluster
      $ReportRow.Host = vmhosts
      }
      $Report += $ReportRow
      }
      $Report | Export-CSV c:\export.csv
      any help would be appreciated
        • 1. Re: help with simple script to list cluster and hosts in the cluster
          Grzesiekk Expert

          Hi,

          this should do it.

          Connect-VIServer xxxxxx
          $datacenter = get-datacenter
          $Report = @()
          foreach ($dc in $datacenter){
          $clusters = Get-Cluster -Location $datacenter
              foreach ($cluster  in $Clusters){
                  $vmhosts=Get-VMHost -Location $cluster
                  foreach ($vmhost in $vmhosts){
                      $ReportRow = "" | Select-Object Datacenter,Cluster, vmHost

                      $ReportRow.Datacenter = $dc.name
                      $ReportRow.Cluster = $cluster.name
                      $ReportRow.vmHost = $vmhost.name
                      $Report += $ReportRow
                  }
              }
          }
          $Report | Export-CSV c:\export.csv

           

          before exporting to csv, you can adjust $report like you want with the sort -property

          $report|sort -property cluster | expoert-csv c:\file.csv

           

          "

          $ReportRow.Cluster = Cluster
          $ReportRow.Host = vmhosts
          "
          This assignment will make error, since you are not getting the cluster name from an current object in your loop. You need to point it to othe vmhost object, from which you will get the name. And from outer loop you have always $cluster in iteration, so you can use it in the inner loop. I have also added you the datacenter property, so you could see it.
          Hope that helps
          GReg
          • 2. Re: help with simple script to list cluster and hosts in the cluster
            jaydo123 Enthusiast

            Sorry this gave me the information I needed

             

             

            foreach($cluster in Get-Cluster){
                 $cluster.Name
                 $cluster | Get-VMHost | select name
            }

            gives me the information I needed