#Author: Anil Narayanan
#This script has been tested on Virtual Center 2.x and vSphere 4.x
#The old script has been re-written from scratch and this is more optimized and a cleaner looking script. There is some amount of redudant code which I plan to remove soon.
#The output of this script is written to a CSV file under C:\ with the name of the Virtual Center
#Import the CSV file and save it as an excel sheet (Select "," and ";" as delimiter)
#Below is the data captured by the script:
#ESX: ESX Host Name, Version Build, Manufacturer, Model, ProcessorType, Total Memory(GB), Uptime (Days), Critical Host Patches, Non-Critical Host Patches, Installed Patches
#VM: VM Name, Power State, H/W Version, CPU's, Memory, OS Name, Tools Status, IP Address, Hard Disks, Datastore Association, OS Disk Utilization(GB), Avg. CPU% Util. (Weekly), Avg. MEM% Util. (Weekly)
clear
add-pssnapin vmware.vimautomation.core
add-pssnapin vmware.vumautomation
$esx_header = "ESX Host Name" + ";" + "Version" + ";" + "Build" + ";" + "Manufacturer" + ";" + "Model" + ";" + "ProcessorType" + ";" + "Total Memory (GB)" + ";" + "Uptime (Days)" + ";" + "Critical Host Patches" + ";" + "Non-Critical Host Patches" + ";" + "Installed Patches (PatchID,Install Date,Desc.)"
$vm_header = "" + ";" + "VM Name" + ";" + "Power State" + ";" + "Version" + ";" + "CPU's" + ";" + "Memory" + ";" + "OS Name" + ";" + "Tools Status" + ";" + "IP Address" + ";" + "Hard Disks" + ";" + "Datastore Association" + ";" + "OS Disk Utilization (GB)" + ";" + "Avg. CPU% Util. (Weekly)" + ";" + "Avg. MEM% Util. (Weekly)"
#Variable Declaration/Initialization
$vcsession=""
$allhosts = @()
$allvms=@()
$vcname=Read-Host("Enter VC Server Name: ") #Accepting Virtual Center Server Name
$vcname = $vcname.toUpper()
$outputfilename="c:\" + $vcname + ".csv"
$vmipaddress=""
$vmdatastores=""
$strvmdatastores=""
$disk_extensiondata=""
$hostpatchlist=""
$allvms = Get-VM | Sort-Object #Get all VM's associated with the Virtual Center
$allhosts = Get-VMHost | Sort-Object #Get all ESX Host's associated with the Virtual Center
#Connecting to Virtual Center Server
$vcsession=Connect-VIServer $vcname
write-output $vcsession
Start-Sleep -Seconds 2
#Send Virtual Center title to file
write-output ("Virtual Center" + ";" + $vcname) > $outputfilename
#Module to fetch VM information
for($j=0;$j -lt $allhosts.Length;$j++)
{
$allhosts[$j].Name
#Send Title header to file
write-output $esx_header >> $outputfilename
if(($vcsession.Version).Contains("4.1"))
{
#The below loop captures patches installed on an ESX server in the variable 'hostpatchlist'
for($i=0;$i -lt ($allhosts[$j] | get-vmhostpatch).Count;$i++)
{
$hostpatchlist=$hostpatchlist + (($allhosts[$j] | get-vmhostpatch)[$i].Id + ", " + ($allhosts[$j] | get-vmhostpatch)[$i].InstallDate + ", " + ($allhosts[$j] | get-vmhostpatch)[$i].Description + "`n" + ";;;;;;;;;;")
}
#Send captured ESX Header information to the CSV file
write-output ( $allhosts[$j].Name + ";" + $allhosts[$j].Version + ";" + $allhosts[$j].Build + ";" + $allhosts[$j].Manufacturer + ";" + $allhosts[$j].Model + ";" + $allhosts[$j].ProcessorType + ";" + [Math]::Round($allhosts[$j].MemoryTotalMB/1024) + ";" + [Math]::Round(((get-date).Date - $allhosts[$j].ExtensionData.Summary.Runtime.Boottime).Totaldays) + ";" + ($allhosts[$j] | Get-Compliance -Baseline (Get-PatchBaseline 'Critical Host Patches')).Status + ";" + ($allhosts[$j] | Get-Compliance -Baseline (Get-PatchBaseline 'Non-Critical Host Patches')).Status + ";" + $hostpatchlist) >> $outputfilename
}
else
{
write-output ( $allhosts[$j].Name + ";" + $allhosts[$j].Version + ";" + $allhosts[$j].Build + ";" + $allhosts[$j].Manufacturer + ";" + $allhosts[$j].Model + ";" + $allhosts[$j].ProcessorType + ";" + [Math]::Round($allhosts[$j].MemoryTotalMB/1024) + ";" + [Math]::Round(((get-date).Date - $allhosts[$j].ExtensionData.Summary.Runtime.Boottime).Totaldays) + ";" + "N/A" + ";" + "N/A" + ";" + "N/A") >> $outputfilename
}
write-output $vm_header >> $outputfilename
for($i=0;$i -lt $allvms.Length;$i++)
{
if($allvms[$i].Host.Name -eq $allhosts[$j].Name) #Compare ESX Server Name as against one of the VM's parameter storing the ESX HostName
{
$allvms[$i].Name
#Populating string with IP Addresses for VM's with many IP Addresses.
for($k=0;$k -lt $allvms[$i].Guest.IPAddress.Count;$k++)
{
$vmipaddress = $vmipaddress + $allvms[$i].Guest.IPAddress[$k] + ", "
}
#Removing the last comma in the IP Address string variable
if(($vmipaddress.Trim()).Length -gt 1)
{
$vmipaddress = $vmipaddress.substring(0,$vmipaddress.Length-2)
}
#Populating string with Datastores for a VM
$vmdatastores = Get-Datastore -VM $allvms[$i].Name
if($vmdatastores.Length)
{
for($k=0;$k -lt $vmdatastores.Length;$k++) #Multiple Disks
{
$strvmdatastores = $strvmdatastores + $vmdatastores[$k] + ", "
}
$strvmdatastores = $strvmdatastores.substring(0,$strvmdatastores.Length-2)
}
else #Single Disk
{
$strvmdatastores = $vmdatastores.Name
}
#Populating string with VM's Disk Utilization
for($k=0;$k -lt (get-vmguest -VM $allvms[$i].Name).ExtensionData.Disk.Count;$k++)
{
$disk_content = "Disk Path: " + (get-vmguest -VM $allvms[$i].Name).ExtensionData.Disk[$k].DiskPath + " Capacity: " + [Math]::Round((((get-vmguest -VM $allvms[$i].Name).ExtensionData.Disk[$k].Capacity)/1073741824),1) + " Free Space: " + [Math]::Round((((get-vmguest -VM $allvms[$i].Name).ExtensionData.Disk[$k].FreeSpace)/1073741824),1) + " / "
$disk_alldisks = $disk_alldisks + $disk_content
}
#Removing the last comma in the Disk Utilization string variable
if(($disk_alldisks).Length -gt 1)
{
$disk_alldisks = $disk_alldisks.substring(0,$disk_alldisks.Length-2)
}
#Sending output to File
write-output ( ";" + $allvms[$i].Name + ";" + $allvms[$i].PowerState + ";" + $allvms[$i].Version + ";" + $allvms[$i].NumCpu + ";" + $allvms[$i].MemoryMB + ";" + $allvms[$i].Guest.OSFullName + ";" + $allvms[$i].Guest.ExtensionData.ToolsStatus + ";" + $vmipaddress + ";" + $allvms[$i].HardDisks.Length + ";" + $strvmdatastores + ";" + $disk_alldisks + ";" + [Math]::Round((Get-Stat -Entity $allvms[$i].Name -Start (Get-Date).AddDays(-7) -Stat cpu.usage.average | Measure-Object Value -Average).Average, 2) + ";" + [Math]::Round((Get-Stat -Entity $allvms[$i].Name -Start (Get-Date).AddDays(-7) -Stat mem.usage.average | Measure-Object Value -Average).Average,2)) >> $outputfilename
#Reinitializing variable to empty string
$vmipaddress = ""
$strvmdatastores=""
$disk_alldisks = ""
$hostpatchlist = ""
}
}
}
write-output ("Total Hosts" + ";" + $allhosts.Count) >> $outputfilename
write-output ("Total VM's" + ";" + $allvms.Count) >> $outputfilename
Advertisement
SocialVibe