PowerShell csv Reports for VMware vSphere Info via vSphere API/PowerCLI

PowerShell Script Lightning Round!

This will give you reports of your vSphere VMs, Hosts and Datastores in csv files.  You’ll need to have PowerCLI installed and have your Get-ExecutionPolicy set to Unrestricted.  (Set-ExecutionPolicy unrestricted).  Also this will need a modern Windows OS because it uses PowerShell 2.0



$VCServerName = "Your vCenter Server's IP Address Or Hostname"

$VC = Connect-VIServer $VCServerName
# you may need to create this directory but probably not
$WORKINGDIR = “C:\Users\YOURUSERNAME\Desktop\VMINFOWORK”
$VMExportFilePath = “C:\Users\YOURUSERNAME\Desktop\BI-FINALS\vm-final.csv”
$HostExportFilePath = “C:\Users\YOURUSERNAME\Desktop\BI-FINALS\host-final.csv”
$DatastoreExportFilePath = “C:\Users\YOURUSERNAME\Desktop\BI-FINALS\datastore-final.csv”

$VMReport = @()
$HOSTREPORT = @()
$STORAGEREPORT = @()

$VMs = Get-VM | where-object{$_.PowerState -eq “PoweredOn”}
$HOSTS = Get-VMHost | where-object{$_.ConnectionState -eq “Connected”}
$DATASTORES = Get-Datastore

### HOSTS SECTION ###

## TWO DIFFERENT FEEDS ####

## this one pulls new data each day by grabbing host info, not from stats history

# uncomment this stuff if you like it more than the stats method

#ForEach ($ESXIHOST in $HOSTS) {
# $HOSTInfo = {} | Select #Name,Version,Build,Cluster,CpuTotalMhz,CpuUsageMhz,MemoryTotalGB,MemoryUsageGB,Date
# $HOSTInfo.Name = $ESXIHOST.Name
# $HOSTInfo.Version = $ESXIHOST.Version
# $HOSTInfo.Build = $ESXIHOST.Build
# $HOSTInfo.Cluster = $ESXIHOST.Parent
# $HOSTInfo.CpuTotalMhz = $ESXIHOST.CpuTotalMhz
# $HOSTInfo.CpuUsageMhz = $ESXIHOST.CpuUsageMhz
# $HOSTInfo.MemoryTotalGB = $ESXIHOST.MemoryTotalGB
# $HOSTInfo.MemoryUsageGB = $ESXIHOST.MemoryUsageGB
# $HOSTInfo.Date = Get-Date -format “MMddyyyy”
# $HOSTREPORT += $HOSTInfo
# }
# $HOSTREPORT = $HOSTREPORT | Sort-Object Name
# If ($HOSTREPORT -ne “”) {
# $HOSTREPORT | Export-Csv $HostExportFilePath -NoTypeInformation
# }

## this one gets all the stats info from history ###
ForEach ($HOSTERSCHOICE in $HOSTS){
Get-Stat -Entity $HOSTERSCHOICE | export-csv $WORKINGDIR\$HOSTERSCHOICE-host.csv -NoTypeInformation
}

## datastore info

ForEach ($DATASTORE in $DATASTORES){
$DATASTOREInfo = {} | Select Name,CapacityGB,FreeSpaceGB,State,Type,Date
$DATASTOREInfo.Name = $DATASTORE.Name
$DATASTOREInfo.CapacityGB = $DATASTORE.CapacityGB
$DATASTOREInfo.FreeSpaceGB = $DATASTORE.FreeSpaceGB
$DATASTOREInfo.State = $DATASTORE.State
$DATASTOREInfo.Type = $DATASTORE.Type
$DATASTOREInfo.Date = Get-Date -format “MMddyyyy”
$STORAGEREPORT += $DATASTOREInfo
}
$STORAGEREPORT = $STORAGEREPORT | Sort-Object Name
If ($STORAGEREPORT -ne “”) {
$STORAGEREPORT | Export-Csv $DatastoreExportFilePath -NoTypeInformation
}

$VMs = Get-VM | where-object{$_.PowerState -eq “PoweredOn”}

ForEach ($VM in $VMs){
Get-Stat -Entity $VM | export-csv $WORKINGDIR\$VM-virtualmachine.csv -NoTypeInformation
}

## creating final reports

Get-ChildItem $WORKINGDIR\*-virtualmachine.csv | ForEach-Object {Import-Csv $_} | Export-Csv $VMExportFilePath -NoTypeInformation
Get-ChildItem $WORKINGDIR\*-host.csv | ForEach-Object {Import-Csv $_} | Export-Csv $HostExportFilePath -NoTypeInformation
Get-ChildItem $WORKINGDIR\*-datastore.csv | ForEach-Object {Import-Csv $_} | Export-Csv $DatastoreExportFilePath -NoTypeInformation
$VC = Disconnect-VIServer -Confirm:$False

 

2 Comments

Dave (July 8, 2013)

Thanks for this script, could you let me know how I can modify it so it will only return the info for the Hosts and not the VMs

Thanks

maxmouse (July 8, 2013)

If you just want the host info, using the vSphere stat info and nothing else you could use this:

$VCServerName = “Your vCenter Server’s IP Address Or Hostname”

$VC = Connect-VIServer $VCServerName
# you may need to create this directory but probably not
$WORKINGDIR = “C:\Users\YOURUSERNAME\Desktop\VMINFOWORK”
$HostExportFilePath = “C:\Users\YOURUSERNAME\Desktop\BI-FINALS\host-final.csv”
$HOSTREPORT = @()
$HOSTS = Get-VMHost | where-object{$_.ConnectionState -eq “Connected”}

ForEach ($HOSTERSCHOICE in $HOSTS){
Get-Stat -Entity $HOSTERSCHOICE | export-csv $WORKINGDIR\$HOSTERSCHOICE-host.csv -NoTypeInformation
}

Get-ChildItem $WORKINGDIR\*-host.csv | ForEach-Object {Import-Csv $_} | Export-Csv $HostExportFilePath -NoTypeInformation

Comments are closed.