VM Host Stats
@"
===============================================================================
Title: Export-VMHostInfo.ps1
Description: Exports VM Information from vCenter into a .CSV file for importing into anything
Usage: .\Export-VMHosts.ps1
Date: 02/07/2012
===============================================================================
"@
IF (Get-PSSnapin | where {$_.name -eq "VMware.VimAutomation.Core"})
{write-host "VMware.VimAutomation.Core snapin already loaded"}
Else
{
write-host "Loading VMware.VimAutomation.Core snapin"
add-PSSnapin VMware.VimAutomation.Core
write-host "VMware.VimAutomation.Core snapin Loaded"
}
$VirtualCenters= "vmvc1","vmvc2"
$Report = @()
$ExportFilePath = "c:\temp\Export-VMHostInfo.csv"
$smtpServer = "mail.blah.com"
$To = "Kevin Curran <kcurran@blah.com>"
$From = "AD Reporting <ADReporting@blah.com>"
#Need to store creds for local account for Ugly hack bellow to get Serial Number from host
$VMHostCreds = Get-VICredentialStoreItem -file "C:\Scripts\VMWare\HostCredFile.xml"
foreach ($VC in $VirtualCenters)
{
Connect-VIServer $VC
$VMHostMachines = Get-VMHost
#collect VM data for attached list of vms
ForEach ($VMHost in $VMHostMachines) {
$counter ++
Write-Host -ForegroundColor Green "$counter Currently querying $($VMHost.name) "
$VMHostView = $VMHost | Get-View
$AssetTag = $($VMHostView.Hardware.SystemInfo.OtherIdentifyingInfo |
where {$_.IdentifierType.Key -eq "AssetTag" }).IdentifierValue
$Serial = $($VMHostView.Hardware.SystemInfo.OtherIdentifyingInfo |
where {$_.IdentifierType.Key -eq "ServiceTag" }).IdentifierValue
$Location = switch ((Get-Datacenter -VMHost $VMHost).Name)
{
"LADC" {"LA"}
"NYDC" {"NY"}
"CDC" {"Chicago"}
"FDC" {"Florida"}
default {$_}
}
$Annotation = $VMHost | Get-Annotation
$HostInfo = {} | Select "Allocation Code","Asset","Asset Tag","Build Template",
"Business Owner","Change Management","Cluster Group","Date CI Last Audited",
"Department","Description","Disk Drives","DR Target","Drive Partitions",
"Environment","Highly Available","Impact Level","IP Address","Line of Business",
"MAC Address","Maintenance Contract ID","Maintenance Window","Manufacturer",
"Memory","Model","Name","Operating System","Physical Location",
"Primary Support Group","Processor Details","Purchase Date","Resides on Chassis #",
"Resides on Rack #","Retired Date","Serial Number","SLA ID","Supplier","vSphereServer","Warranty Expiration Date"
$HostInfo."Allocation Code" =
$HostInfo."Asset" = "Yes"
$HostInfo."Asset Tag" = $AssetTag
$HostInfo."Build Template" = ""
$HostInfo."Business Owner" = "Operations"
$HostInfo."Change Management" = "Managed"
$HostInfo."Cluster Group" = $VMHost.Parent.Name
$HostInfo."Date CI Last Audited" = ""
$HostInfo."Department" = "Information Technology"
$HostInfo."Description" = ""
$HostInfo."Disk Drives" = ""
$HostInfo."DR Target" = "None"
$HostInfo."Drive Partitions" = ""
$HostInfo."Environment" = "PROD"
$HostInfo."Highly Available" = "No"
$HostInfo."Impact Level" = "Critical"
#Ugly can't find how to get ip from powercli
$HostInfo."IP Address" = (Test-Connection -Count 1 $VMHost.name).IPV4Address.IPAddressToString
$HostInfo."Line of Business" = ""
$HostInfo."MAC Address" = ""
$HostInfo."Maintenance Contract ID" =
$HostInfo."Maintenance Window" ="Every Thursday 9pm-12am"
$HostInfo."Manufacturer" = $VMHost.Manufacturer
$HostInfo."Memory" = $VMHost.MemoryTotalMB
$HostInfo."Model" = $VMHost.Model
$HostInfo."Name" = $VMHost.name
$HostInfo."Operating System" = $VMHostView.Config.Product.FullName
$HostInfo."Physical Location" = $Location
$HostInfo."Primary Support Group" = "Wintel"
$HostInfo."Processor Details" = $VMHost.ProcessorType
$HostInfo."Purchase Date" = ""
$HostInfo."Resides on Chassis #" = ""
$HostInfo."Resides on Rack #" = ""
$HostInfo."Retired Date" = ""
$HostInfo."Serial Number" = $Serial
$HostInfo."SLA ID" = ""
$HostInfo."Supplier" = ""
$HostInfo."vSphereServer" = $VC
$HostInfo."Warranty Expiration Date" = ""
$Report += $HostInfo
}
Disconnect-VIServer -Confirm:$False
}
$Report = $Report | Sort-Object Name
foreach ($Server in $Report)
{
#Ugly Hack because of a bug where vsphere powerCLI intermittently can't get the serial number
#even though it is visible on the Hardware Status tab of the esx host
if ($Server.Serial -eq $null)
{
#Check if Lockdown mode is enable if so disable it
Connect-VIServer $Server.vSphereServer
$VMHost = Get-VMHost -Name $Server.Name
$VMHostView = $VMHost | Get-View
If ($VMHost.ExtensionData.Config.AdminDisabled -eq $true) {$VMHostView.ExitLockdownMode()}
Disconnect-VIServer -Confirm:$False
Connect-VIServer $VMHost.Name -User $VMHostCreds.User -Password $VMHostCreds.Password
#$ESXHostView = Get-Vmhost $VMHost.Name | Get-View
$Server."Asset Tag" = ((Get-Vmhost $VMHost.Name | Get-View).Hardware.SystemInfo.OtherIdentifyingInfo |
where {$_.IdentifierType.Key -eq "AssetTag"}).IdentifierValue
$Server."Serial Number" = ((Get-Vmhost $VMHost.Name | Get-View).Hardware.SystemInfo.OtherIdentifyingInfo |
where {$_.IdentifierType.Key -eq "ServiceTag"}).IdentifierValue
Disconnect-VIServer -Confirm:$False
Connect-VIServer $Server.vSphereServer
#seems like i need to recreate $VMHostView after the disconnect
$VMHostView = $VMHost | Get-View
$VMHostView.EnterLockdownMode()
Disconnect-VIServer -Confirm:$False
}
#Modify name of host to not include domain name
$Server.Name = $Server.name.split(".")[0]
}
#Export VM List Report to a File to attach to email
IF ($Report -ne "")
{
$report | Export-Csv $ExportFilePath -NoTypeInformation
#Send Message
Send-MailMessage -From $From -To $To -SmtpServer $smtpServer `
-Subject "VM Hosts" -Attachments $ExportFilePath
}