Thursday, December 3, 2009

Getting Performance report of VM's from VC - VIToolkit

Below is the script to collect the performance report of the VM's mentioned in the specific folder. It uses VI tool kit, exports the average value to excel and sends a mail.

clear
$excel = new-object -comobject excel.application
$excel.visible = $false
$workbook = $excel.workbooks.add()
$sheet = $workbook.WorkSheets.Item(1)
$sheet.cells.item(1,1) = "Hostname"
$sheet.cells.item(1,2) = "CPU AVG"
$sheet.cells.item(1,3) = "MEM AVG"
$sheet.cells.item(1,4) = "Disk AVG"
$sheet.cells.item(1,5) = "NW AVG"
$x = 2
$strpath = "c:\perf.xls"
Connect-VIServer -server
$start = "MM/DD/YY 00:01"
$finish = "MM/DD/YY 23:59"
$vm = Get-VM -Location (Get-Folder "folder")
foreach($v in $vm)
{
$cpuavg = get-stat -Entity $v -Start $start -Finish $finish -Stat cpu.usage.average | `
Measure-Object -Property value -Average
$memavg = get-stat -Entity $v -Start $start -Finish $finish -Stat mem.usage.average |`
Measure-Object -Property value -Average
$diskavg = get-stat -Entity $v -Start $start -Finish $finish -Stat disk.usage.average |`
Measure-Object -Property value -Average
$netavg = get-stat -Entity $v -Start $start -Finish $finish -Stat net.usage.average |`
Measure-Object -Property value -Average
$sheet.cells.item($x,1) = $v.name
$sheet.cells.item($x,2) = $cpuavg.average
$sheet.cells.item($x,3) = $memavg.average
$sheet.cells.item($x,4) = $diskavg.average
$sheet.cells.item($x,5) = $netavg.average
$x++
}
$range = $sheet.usedRange
$range.EntireColumn.AutoFit()
out-null
IF(Test-Path $strPath)
{
Remove-Item $strPath
$Excel.ActiveWorkbook.SaveAs($strPath)
}
ELSE
{
$Excel.ActiveWorkbook.SaveAs($strPath)
}
$excel.quit()
spps -name excel
$filename = “c:\perf.xls”
$smtpServer = “SMTP server”
$msg = new-object Net.Mail.MailMessage
$att = new-object Net.Mail.Attachment($filename)
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$msg.From = “mail@domain.com”
$msg.To.Add(”mail@domain.com”)
$msg.Subject = “”
$msg.Body = “”
$msg.Attachments.Add($att)
$smtp.Send($msg)

No comments: