Virtually everything is poshable
PowerCLI: SnapReminder
Fed up of chasing those people who constantly create snapshots and leave them hanging around for weeks or even months on end ?
You no longer have to do the chasing, just use the following script to automatically find the offending snapshot, find the person who created it, get their email address from AD and send them an email reminding them of their mortal sin.
The email address is taken from the Email field as shown below:
A few requirements:
- The accounts must have the E-Mail field filled out
- The account you run the script as must have read permissions to AD (Any member of the domain should have this)
- You need to fill in the smtp server address, VI Server name and the from address at the top of this script
- You can run this as a scheduled task to constantly email the offending parties
- The below script is set to remind of anything over 2 weeks old but this can easily be amended
- Once completed the offending person will receive an email as below:
Thanks to LucD for helping with finding the user who created the snapshot on the PowerCLI Forum.
The Script:
# - SnapReminder V1.0 By Virtu-Al - http://virtu-al.net
#
# Please use the below variables to define your settings before use
#
$smtpServer = "mysmtpserver.mydomain.com"
$MailFrom = "me@mydomain.com"
$VISRV = "MYVISERVER"
function Find-User ($username){
if ($username -ne $null)
{
$usr = (($username.split("\"))[1])
$root = [ADSI]""
$filter = ("(&(objectCategory=user)(samAccountName=$Usr))")
$ds = new-object system.DirectoryServices.DirectorySearcher($root,$filter)
$ds.PageSize = 1000
$ds.FindOne()
}
}
function Get-SnapshotTree{
param($tree, $target)
$found = $null
foreach($elem in $tree){
if($elem.Snapshot.Value -eq $target.Value){
$found = $elem
continue
}
}
if($found -eq $null -and $elem.ChildSnapshotList -ne $null){
$found = Get-SnapshotTree $elem.ChildSnapshotList $target
}
return $found
}
function Get-SnapshotExtra ($snap){
$guestName = $snap.VM # The name of the guest
$tasknumber = 999 # Windowsize of the Task collector
$taskMgr = Get-View TaskManager
# Create hash table. Each entry is a create snapshot task
$report = @{}
$filter = New-Object VMware.Vim.TaskFilterSpec
$filter.Time = New-Object VMware.Vim.TaskFilterSpecByTime
$filter.Time.beginTime = (($snap.Created).AddSeconds(-5))
$filter.Time.timeType = "startedTime"
$collectionImpl = Get-View ($taskMgr.CreateCollectorForTasks($filter))
$dummy = $collectionImpl.RewindCollector
$collection = $collectionImpl.ReadNextTasks($tasknumber)
while($collection -ne $null){
$collection | where {$_.DescriptionId -eq "VirtualMachine.createSnapshot" -and $_.State -eq "success" -and $_.EntityName -eq $guestName} | %{
$row = New-Object PsObject
$row | Add-Member -MemberType NoteProperty -Name User -Value $_.Reason.UserName
$vm = Get-View $_.Entity
$snapshot = Get-SnapshotTree $vm.Snapshot.RootSnapshotList $_.Result
$key = $_.EntityName + "&" + ($snapshot.CreateTime.ToString())
$report[$key] = $row
}
$collection = $collectionImpl.ReadNextTasks($tasknumber)
}
$collectionImpl.DestroyCollector()
# Get the guest's snapshots and add the user
$snapshotsExtra = $snap | % {
$key = $_.vm.Name + "&" + ($_.Created.ToString())
if($report.ContainsKey($key)){
$_ | Add-Member -MemberType NoteProperty -Name Creator -Value $report[$key].User
}
$_
}
$snapshotsExtra
}
Function SnapMail ($Mailto, $snapshot)
{
$msg = new-object Net.Mail.MailMessage
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$msg.From = $MailFrom
$msg.To.Add($Mailto)
$msg.Subject = "Snapshot Reminder"
$MailText = @"
This is a reminder that you have a snapshot active on $($snapshot.VM) which was taken on $($snapshot.Created).
Name: $($snapshot.Name)
Description: $($snapshot.Description)
"@
$msg.Body = $MailText
$smtp.Send($msg)
}
Connect-VIServer $VISRV
foreach ($snap in (Get-VM | Get-Snapshot | Where {$_.Created -lt ((Get-Date).AddDays(-14))})){
$SnapshotInfo = Get-SnapshotExtra $snap
$mailto = ((Find-User $SnapshotInfo.Creator).Properties.mail)
SnapMail $mailto $SnapshotInfo
}
| Print article | This entry was posted by Virtu-Al on June 22, 2009 at 13:39, and is filed under powershell, vmware. Follow any responses to this post through RSS 2.0. You can leave a response or trackback from your own site. |
- VMware vSphere 4 has a Snapshot Alarm | VCritical
- Virtu-Al » PowerCLI: SnapReminder « ben.neise.co.uk
- VMware Scripting: Snapshots auf der Spur
- How to ease the management and monitoring of VMware Snapshots « TheSaffaGeek
- How to ease the management and monitoring of VMware Snapshots « TheSaffaGeek
- How to ease the management and monitoring of VMware Snapshots « TheSaffaGeek
- How to ease the management and monitoring of VMware Snapshots « TheSaffaGeek
- VMware Snapshot Alerting and Reporting | VirtualPro







(3)
(13)
(0)
about 1 year ago
Hey Allan, a suggestion it would be nice to see the size of the snapshot file(s) in GB the e-mail
about 1 year ago
Love it!
about 1 year ago
@Ivo Beerens
Good thinking V2 Coming up !
about 1 year ago
@Jason Boche
Thanks man
about 1 year ago
I get an error when running the script error line 93 char 13
+ $MailText = @ <<<< "
If I remove the @ I get another error with another @ symbol farther down.
about 1 year ago
@chris
My blog mangled the code, I have now moved it over to a different hosting method so please try the above.
Let me know if you have any further issues.
about 9 months ago
Good script,thanks. I had a problem with the timing, so I changed the line 50 to:
$filter.Time.beginTime = (($snap.Created).AddMinutes(-30)
about 9 months ago
Well, for me it is semi-working. I get emails, but the script gives me the following error:
You cannot call a method on a null-valued expression.
At :line:63 char:83
+ $key = $_.EntityName + “&” + ($snapshot.CreateTime.ToString <<<< ())
Index operation failed; the array index evaluated to null.
At :line:64 char:32
+ $report[ <<<< $key] = $row
about 9 months ago
@Falko
I had this error also. In my case it was because the Administrator made the snapshot an there is no email adress for Administrator in the AD. My Solution:
Line 10:
if ($username -ne $null -and $username -ne “Administrator”)
Line 86:
if($Mailto -eq $null){
$msg.To.Add(“vmadmin@blah.bla”)
}
else{
$msg.To.Add($Mailto)
}
Hth
about 4 months ago
Where do you stand on a v2 of the snapshot reminder?
about 1 month ago
Excellant write-up! Thanks for the tip, works like a charm! Nagging has begun!