Saturday, March 5, 2016

VMware PowerCLI get detailed VM report in simple way and export to Excel sheet

#VM Report
clear
Add-PSSnapin VMware.VimAutomation.Core
$Admin ="tech-jockey\jockey-admin"
$Password = Get-Content c:\vccred\securestring.txt | convertto-securestring
$Credential = new-object -typename System.Management.Automation.PSCredential -argumentlist $Admin, $Password
Connect-VIServer tech-jockey-vCenter -SaveCredentials -Credential $Credential

$Excel = New-Object -Com Excel.Application
$Excel.visible = $False
$Excel = $Excel.Workbooks.Add()
$Addsheet = $Excel.sheets.Add()
$Sheet = $Excel.WorkSheets.Item(1)
$Sheet.Cells.Item(1,1) = “VM Name”
$Sheet.Cells.Item(1,2) = “CPU”
$Sheet.Cells.Item(1,3) = “RAM”
$Sheet.Cells.Item(1,4) = “Status”
$Sheet.Cells.Item(1,5) = “Folder”
$Sheet.Cells.Item(1,6) = “OS”
$Sheet.Cells.Item(1,7) = “HardDisks”
$Sheet.Cells.Item(1,8) = “Total Space”
$Sheet.Cells.Item(1,9) = “Used Space”
$Sheet.Cells.Item(1,10) = “Physical Host”
$Sheet.Cells.Item(1,11) = “|”
$WorkBook = $Sheet.UsedRange
$WorkBook.Font.Bold = $True
$intRow = 2

$colItems=get-vm
foreach ($objItem in $colItems)
{
$Sheet.Cells.Item($intRow,1) = $objItem.Name

 $powerstate = $objItem.PowerState
If ($PowerState -eq 1) {$power = “Powerd On”}
Else {$power = “Powerd Off”}
$folder1= $objItem.Folder.ToString()
$Guest1 = $objItem.Guest.ToString()
$HardDisks1 = $objItem.HardDisks.Filename

$allhdd=""
$sep=""
foreach($_ in $HardDisks1)
{
$allhdd=$allhdd.ToString() +$sep.ToString()+$_.ToString()
$sep=","

}
$ProvisionedSpaceGB1 = $objItem.ProvisionedSpaceGB.ToString()
$UsedSpaceGB1 = $objItem.UsedSpaceGB.ToString()
$VMHost1 = $objItem.VMHost.Name.ToString()
$Sheet.Cells.Item($intRow,2) = $objItem.NumCPU
$Sheet.Cells.Item($intRow,3) = $objItem.MemoryMB
$Sheet.Cells.Item($intRow,4) = $power
$Sheet.Cells.Item($intRow,5) = $folder1
$Sheet.Cells.Item($intRow,6) = $Guest1
$Sheet.Cells.Item($intRow,7) = $allhdd
$Sheet.Cells.Item($intRow,8) = $ProvisionedSpaceGB1
$Sheet.Cells.Item($intRow,9) = $UsedSpaceGB1
$Sheet.Cells.Item($intRow,10) = $VMHost1

$intRow = $intRow + 1

}

$WorkBook.EntireColumn.AutoFit()

$dt=get-date

$day=$dt.Day.ToString()
$hour=$dt.Hour.ToString()
$min=$dt.Minute.ToString()
$month=$dt.Month.ToString()
$sec=$dt.Second.ToString()
$yr=$dt.Year.ToString()
$filename="VM_Report_"+$day+"_"+$month+"_"+$yr+"_"+$hour+"_"+$min+"_"+$sec

$filepath="C:\jockey\"+$filename+".xlsx"

echo $filepath
echo "1"

$Excel.SaveAs($filepath)
$excel.Close()

No comments:

Post a Comment

Thanks for showing interest in tech-jockey.

Content of this blog has been moved to GITHUB

Looking at current trends and to make my content more reachable to people, I am moving all the content of my blog https://tech-jockey.blogsp...