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
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.
-Stuart