PowerCLI get Hostname and IP for all VMs in environment

I needed a way to get a dump of all the VMs in the environment with their IPs neatly tucked into a single file.  So I developed this script to do it.

$CSVOut = ".\Server_IP_List.csv"
Write-Host "Building VM List..."
$vm = Get-VM
write-host "Building Variable List..."
$vmview = $vm | Get-View

$CSV = "Server Name, IPAddress 1, IPAddress 2 `n"
Foreach ($v in $vmview){
    $Name = $v.Guest.HostName
    If ($Name -ne $null -and $Name -ne ""){
        $CSV += $Name
        Foreach ($nic in $v.Guest.Net){
            $IP = $nic.IPAddress
            $CSV += ","
            $CSV += $IP
            }
        }
    Else {
        $CSV += $vmview.Name
        }
    $CSV += "`n"
    }
Write-Host "Writing file $CSVOut"
Write "$CSV" | Out-File -Encoding ASCII $CSVOut -NoClobber -append


You need to ensure you are firstly connected to the vCenter before running the script but it outputs a great little CSV.  

I am still trying to get some the spaces to not be there but that is easily corrected in Excel.

Comments