Get vMotion IPs from all VMHost

I was recently asked to gather the vMotion IPs for all of the hosts in our environment. We have about 80 or so VM Hosts, so this was a job for PowerCli.  Below is the script.

$Report = @()

$Clusters = Get-Cluster | Sort Name

ForEach ($Cluster in $Clusters){
    $VmHosts = $Cluster | Get-VmHost | Where {$_.ConnectionState -eq “Connected”} | Sort Name
        ForEach ($VmHost in $VmHosts){
            $Report += Get-VMHostNetworkAdapter -VMHost $VmHost.Name -VMKernel | Where {$_.VMotionEnabled -eq “True”} | select VmHost,IP
        }
}
$Report | Export-Csv C:\Scripts\Logs\vMotionIPs.csv -NoTypeInformation -UseCulture
vMotion IPs Gather from the Script
vMotion IPs
vMotion IPs gather with PowerCLI

First there needs to be a place to put all of the information that is going to be collected.  So all of the information will be stored in an array.

 $Report = @()

Then we need to gather all of the clusters, which if your environment doesn’t use clusters look below for a script that will work for you.  Once all of the Clusters are gather in the variable, I now need to start processing them one at a time to make sure that they are connected.

ForEach ($Cluster in $Clusters){
    $VmHosts = $Cluster | Get-VmHost | Where {$_.ConnectionState -eq “Connected”} | Sort Name

Then process them once more to get the required information and put it into the array.

ForEach ($VmHost in $VmHosts){
            $Report += Get-VMHostNetworkAdapter -VMHost $VmHost.Name -VMKernel | Where {$_.VMotionEnabled -eq “True”} | select VmHost,IP
        }

By using the switch ‘-VMKernel‘ this limits the type of Network Adapters that is checked for the ‘VMotionEnabled‘.

$Report | Export-Csv C:\Scripts\Logs\vMotionIPs.csv -NoTypeInformation -UseCulture

This last line is what writes the results to a Csv file.  Using the ‘-NoTypeInformation’ prevents the type information from being placed on the first line of the CSV so this makes it easier to feed into another script.

Export-CSV Type information
Export-CSV Type information

-Stuart

2 thoughts on “Get vMotion IPs from all VMHost”

    1. Zimm,
      That is a good question, and the answer is it will still work, but the output will need to be modified otherwise you will get System.Object[]. To resolve this issue you will need to modify the script like the following:

      $Report = @()
       
      $Clusters = Get-Cluster | Sort Name
       
      ForEach ($Cluster in $Clusters){
          $VmHosts = $Cluster | Get-VmHost | Where {$_.ConnectionState -eq “Connected”} | Sort Name
              ForEach ($VmHost in $VmHosts){
                  $line           = "" | Select VMhost, vMotionIP
                  $line.VMhost    = $VMhost.name
                  $line.vMotionIP = (@((Get-VMHostNetworkAdapter -VMHost $VmHost.Name -VMKernel | Where {$_.VMotionEnabled -eq “True”}).IP) -join ', ')
                  $Report        += $line
              }
      }
      $Report | Export-Csv C:\Scripts\Logs\vMotionIPs.csv -NoTypeInformation -UseCulture
      

      I hope this helps.
      – Stuart

Leave a Reply

Your email address will not be published. Required fields are marked *