Vmware Snapshot List

#############################################################################
# List all VM with snapshots
# Run as scheduled task with a user with rights to view all VMs
#

Add-PSSnapin VMware.VimAutomation.Core

# HTML/CSS style for the output file
$head = “<style>”
$head = $head + “BODY{background-color:white;}”
$head = $head + “TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}”
$head = $head + “TH{border-width: 1px;padding: 0px;border-style: solid;border-color: black;background-color:thistle}”
$head = $head + “TD{border-width: 1px;padding: 0px;border-style: solid;border-color: black;background-color:PaleGoldenrod}”
$head = $head + “</style>”

# SMTP info
$smtpServer = “smtp.domain.com”
$strFrom = “[email protected]
$strTo = “[email protected]
$strSubject = “Snapshot list – ” + (get-date -DisplayHint date)
$strBody = “Attached is the list of Snapshots”
$strMail = “<H2><u>” + $strSubject + “</u></H2>”

# List your vCenter servers in quotes separated by commas
$Servers=”VcenterServer1″,”VcenterServer2″
foreach ($Server in $Servers){
Connect-VIServer $Server
$date=Get-Date -uFormat “%Y%m%d%H%M%S”
$strOutFile = “C:\Scripting\Output\snapshot_list$date.htm”
$strSubject = “Snapshot list $Server – ” + (get-date -DisplayHint date)

# Get the list of VM’s
$vms = Get-VM

$myCol = @()
ForEach ($vm in $vms){
$snapshots = Get-SnapShot -VM $vm
if ($snapshots.Name.Length -ige 1 -or $snapshots.length){
ForEach ($snapshot in $snapshots){
$myObj = “” | Select-Object VM, Snapshot, Created, Description
$myObj.VM = $vm.name
$myObj.Snapshot = $snapshot.name
$myObj.Created = $snapshot.created
$myObj.Description = $snapshot.description
$myCol += $myObj
}
}
}

# Write the output to an HTML file
if ($myCol -ne $null){
$myCol | Sort-Object VM | ConvertTo-HTML -Head $head -Body $strMail | Out-File $strOutFile

# Mail the output file
$msg = new-object Net.Mail.MailMessage
$att = new-object Net.Mail.Attachment($strOutFile)
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$msg.From = $strFrom
$msg.To.Add($strTo)
$msg.Subject = $strSubject
$msg.IsBodyHtml = 1
$msg.Body = Get-Content $strOutFile
$msg.Attachments.Add($att)
$msg.Headers.Add(“message-id”, “<3BD50098E401463AA228377848493927-1>”) # Adding a Bell Icon for Outlook users

$smtp.Send($msg)}
DisConnect-VIServer -Server * -Force -Confirm:$false
}

No Comments

Post a Comment