### -----------------------------------------------------------------
### Written by Matt Brown
###
### Description: Powershell script grabs a list of snapshots from the
### VMWare Enviornment and emails them out as a report.
###
### Requires: VMWare Powershell Extenstions
### -----------------------------------------------------------------
$thedate = Get-Date -f yyyy-MM-dd_HH-mm
$scriptname = "VMWareSnapshots.ps1"
$scriptlocation = "C:\Scripts\VMWare\"
$filename = $scriptlocation + "Transcripts\" + $thedate + "_Snapshots.rtf"
start-transcript -path $filename
$htmlOutFile = "C:\Scripts\VMWare\Reports\snapshot_list.htm"
$vCenterServer = "VC.domain.com"
$vCenterLocation = "ProductionCluster"
### Load VMWare Snapin
Add-PSSnapin -Name VMware.VimAutomation.Core
### -----------------------------------------------------------------
### Start Functions
### -----------------------------------------------------------------
function SendEmail($body,$subject=("Script ERROR: " + $scriptname + " on " + ($env:COMPUTERNAME)),$to=@("admin@domain.com"),$attFile=$false) {
$message = New-Object System.Net.Mail.MailMessage
if($attFile) {
$attachement = New-Object System.Net.Mail.Attachment($attFile)
$message.Attachments.Add($attachement)
$message.Headers.Add("message-id", "<3BD50098E401463AA228377848493927-1>") # Adding a Bell Icon for Outlook users
}
$message.From = "admin@domain.com"
$to | foreach {
$message.To.Add($_) # default is admin in function
}
$message.Subject = $subject
$bodyh = "----------------------------------------------------------------------------------------------------`n"
$bodyh += "Server: " + ($env:COMPUTERNAME) + "`n"
$bodyh += "User: " + ($env:USERDOMAIN) + "\" + ($env:USERNAME) + "`n"
$bodyh += "Location: " + $scriptlocation + $scriptname + "`n"
$bodyh += "----------------------------------------------------------------------------------------------------`n`n"
$message.Body = $bodyh + $body
$smtp = New-Object System.net.Mail.SmtpClient
$smtp.Host = "smtpserver.domain.com"
$smtp.UseDefaultCredentials = $true
$smtp.Send($message)
}
### -----------------------------------------------------------------
Connect-VIServer $vCenterServer
# HTML/CSS style for the output file
$head = ""
$title = ($vCenterLocation + " VMWare Snapshots as of ” + (get-date -Format "MM-dd-yyyy"))
$data = @()
Get-VM -Location $vCenterLocation | foreach {
$snapshots = Get-SnapShot -VM $_
if ($snapshots.Name.Length -ige 1 -or $snapshots.length){
ForEach ($snapshot in $snapshots){
$myObj = "" | Select-Object VM, Snapshot, Created, Description
$myObj.VM = $_.name
$myObj.Snapshot = $snapshot.name
$myObj.Created = $snapshot.created
$myObj.Description = $snapshot.description
$data += $myObj
}
}
}
# Write the output to an HTML file
$data | Sort-Object VM | ConvertTo-HTML -Head $head -Body (""+$title+"
") | Out-File $htmlOutFile
SendEmail ("See Attached VMWare Snapshot Report") $title (@("joe@domain.com","fred@domain.com")) $htmlOutFile
DisConnect-VIServer -Confirm:$false
stop-transcript
Ramblings from University IT... VMWare, NetApp, Powershell,Active Directory, Exchange and Scripting.
Monday, November 3, 2014
Powershell: VMWare Snapshot Report
Here's a handy script I use to send me a report on all VM's in my VMWare Environment with active snapshots. This script finds all VM's with Snapshots, creates an HTML report and emails the info to the provided list. I schedule this to run via the Task Manager automatically every week.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment