Wednesday, June 15, 2011

Gathering VM inventory via PowerShell

We are moving several VMs from an ESX4.0 environment to a ESX4.1 environment and performing tools/HW upgrades at the same time. The two environments report to different Virtual Center servers, so we need to collect various information about the VMs prior to migrating them. Below is a combination of several scripts I found online to provide me the Folder Path, VM version, and Network information from all VMs in a VC. Its not perfect, but it provides the preliminary information needed to begin moving VMs


$Information = @()
Foreach ($VM in (Get-VM | Sort Name)){
$VMv= $VM | Get-View
$MyDetails = "" | Select-Object VMName,VMXLocation,FolderPath,Version,NetworkName
$MyDetails.VMName = $VMv.Name
$MyDetails.VMXLocation = $VMv.Config.Files.VmPathName
$current = get-view $vmv.Parent
$path = $vmv.Name
do {
 $parent = $current
 if($parent.name -ne "vm"){$path = $parent.Name + "\" + $path}
 $current = Get-View $current.Parent
} while ($current.Parent -ne $null)
$MyDetails.FolderPath = $path
$MyDetails.Version = $VM.Version
#$MyDetails.NetworkName = foreach ($nic in $VM.Guest.NICs) { $nic.Connected.tostring() +" "+ $nic.NetworkName +" "+ $nic.IPAddress }
foreach ($nic in $VM.Guest.NICs) { $MyDetails.NetworkName += $nic.Connected.tostring() +","+ $nic.NetworkName +","+ $nic.IPAddress +"," }
#$MyDetails
$Information += $MyDetails
}
$Information 
$Information | Export-Csv d:\temp\output.csv

No comments: