Every vSphere Admin knows, that if you give someone in your organization the rights to take snapshots, you will lose the control of them.
Often they forget to delete the snapshot after their maintenance. So the snapshots gets bigger and bigger. The results are full datastores, big snapshots that can’t be no-more deleted and in the worst case you will have corrupted VM.
To counter against those problems, I’ve wrote a Powershell script that gets all snapshots in your environment and sends you an email with the name, size, time and the description of the snapshots.
You just have to edit the global variables with yours and then schedule the script.
After this, you can control your snapshots much more better.
Feel free to use, edit and share it:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
$vCenterServer = 'localhost' $Subject = 'Weekly Get-Snapshots' $Recipient = 'email@your.org' $BCC = 'email2@your.org' $EMailDomain = 'your.org' $SMTPServer = 'smtp.your.org' #global vars +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ [array]$global:AllSnapShots [array]$global:AllSnapShotsformated #HTML formatting vars+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ $html = "<style>" $html = $html + "TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}" $html = $html + "TH{border-width: 1px;padding: 0px;border-style: solid;border-color: black;}" $html = $html + "TD{border-width: 1px;padding: 0px;border-style: solid;border-color: black;}" $html = $html + "</style>" #functions +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ #function to load the VMware Snapin function LoadSnapin { param ( $Snapin) process { If ((Get-PSSnapin $Snapin -ErrorAction SilentlyContinue) -eq $null) { Write-Host Loading Snapin $Snapin Add-PSSnapin $Snapin -WarningAction SilentlyContinue } } } #function to connect to the vCenter Server function ConnectVI { param ( $vCenterServer) Write-Host 'Connecting to vCenter' $vCenterServer Connect-VIServer $vCenterServer -WarningAction SilentlyContinue } #Function to get all Snapshots in vCenter function Get-Snapshots { #Gets all Snapshots in vCenter $global:AllSnapShots = Get-VM | Get-Snapshot #Formats the Output of the get-snapshot for the HTML Mail #The Hash is to format the SizeinGB to only have two decimal numbers $global:AllSnapShotsformated = $AllSnapShots | select VM, Created, @{l='SizeInGB';e={"{0:N2}" -f ($_.SizeGB)}}, Name, Description | sort VM | ConvertTo-Html -head $html } #The function send-mail uses the PS-Command send-mailmessage to send the final mail function Send-Mail { #If there are some snapshots in $allsnapshots it mails als snapshots that are presented. else it just sends a mail with the Thext "No Snapshot found" if ($AllSnapShots) { Send-MailMessage -bodyashtml:$true -Body "$AllSnapShotsformated" -from $env:computername"@$EMailDomain" -SmtpServer $SMTPServer -Subject $Subject -To $Recipient -Bcc $BCC -Priority High } else { Send-MailMessage -bodyashtml:$true -Body "No Snapshots found" -from $env:computername"@$EMailDomain" -SmtpServer $SMTPServer -Subject $Subject -To $Recipient -Bcc $BCC -Priority low } } #function to disconnect to the vCenter Server function DisconnectVI { param ( $vCenterServer) Write-Host 'Disconnecting from vCenter' $vCenterServer Disconnect-VIServer $vCenterServer -Confirm:$false -WarningAction SilentlyContinue } # Main ++++++++++++++++++++++++++++++++++++++++++++++ LoadSnapin 'VMware.VimAutomation.Core' ConnectVI $vCenterServer Get-Snapshots Send-Mail DisconnectVI $vCenterServer exit 0 |
Hi Bruno,
thanks for your work.
would you mind sharing with us re running it from, vcentre server, esxi?
thanks
I have made a scheduled task on the vcenter that runs this script once a week.
Dont know if this is really what you are asking for 🙂
all good, seems to be working except i am getting email with those info:
TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}TH{border-width: 1px;padding: 0px;border-style: solid;border-color: black;}TD{border-width: 1px;padding: 0px;border-style: solid;border-color: black;}
VM Created SizeInGB Name Description
This is the HTML format of the email. Do you have disabled html mails on your mail server or on your mail client?