PoshCode Logo PowerShell Code Repository

SnapReminder by alanrenouf 31 months ago
embed code: <script type="text/javascript" src="http://PoshCode.org/embed/1189"></script>download | new post

Remind the users of their snapshots – for use in VMware, see this post for more details: http://www.virtu-al.net/2009/06/22/powercli-snapreminder/

  1. # - SnapReminder V1.0 By Virtu-Al - http://virtu-al.net
  2. #
  3. # Please use the below variables to define your settings before use
  4. #
  5. $smtpServer = "mysmtpserver.mydomain.com"
  6. $MailFrom = "me@mydomain.com"
  7. $VISRV = "MYVISERVER"
  8.  
  9. function Find-User ($username){
  10.         if ($username -ne $null)
  11.         {
  12.                 $usr = (($username.split("\"))[1])
  13.                 $root = [ADSI]""
  14.                 $filter = ("(&(objectCategory=user)(samAccountName=$Usr))")
  15.                 $ds = new-object system.DirectoryServices.DirectorySearcher($root,$filter)
  16.                 $ds.PageSize = 1000
  17.                 $ds.FindOne()
  18.         }
  19. }
  20.  
  21. function Get-SnapshotTree{
  22.         param($tree, $target)
  23.        
  24.         $found = $null
  25.         foreach($elem in $tree){
  26.                 if($elem.Snapshot.Value -eq $target.Value){
  27.                         $found = $elem
  28.                         continue
  29.                 }
  30.         }
  31.         if($found -eq $null -and $elem.ChildSnapshotList -ne $null){
  32.                 $found = Get-SnapshotTree $elem.ChildSnapshotList $target
  33.         }
  34.        
  35.         return $found
  36. }
  37.  
  38. function Get-SnapshotExtra ($snap){
  39.         $guestName = $snap.VM   # The name of the guest
  40.  
  41.         $tasknumber = 999               # Windowsize of the Task collector
  42.        
  43.         $taskMgr = Get-View TaskManager
  44.        
  45.         # Create hash table. Each entry is a create snapshot task
  46.         $report = @{}
  47.        
  48.         $filter = New-Object VMware.Vim.TaskFilterSpec
  49.         $filter.Time = New-Object VMware.Vim.TaskFilterSpecByTime
  50.         $filter.Time.beginTime = (($snap.Created).AddSeconds(-5))
  51.         $filter.Time.timeType = "startedTime"
  52.        
  53.         $collectionImpl = Get-View ($taskMgr.CreateCollectorForTasks($filter))
  54.        
  55.         $dummy = $collectionImpl.RewindCollector
  56.         $collection = $collectionImpl.ReadNextTasks($tasknumber)
  57.         while($collection -ne $null){
  58.                 $collection | where {$_.DescriptionId -eq "VirtualMachine.createSnapshot" -and $_.State -eq "success" -and $_.EntityName -eq $guestName} | %{
  59.                         $row = New-Object PsObject
  60.                         $row | Add-Member -MemberType NoteProperty -Name User -Value $_.Reason.UserName
  61.                         $vm = Get-View $_.Entity
  62.                         $snapshot = Get-SnapshotTree $vm.Snapshot.RootSnapshotList $_.Result
  63.                         $key = $_.EntityName + "&" + ($snapshot.CreateTime.ToString())
  64.                         $report[$key] = $row
  65.                 }
  66.                 $collection = $collectionImpl.ReadNextTasks($tasknumber)
  67.         }
  68.         $collectionImpl.DestroyCollector()
  69.        
  70.         # Get the guest's snapshots and add the user
  71.         $snapshotsExtra = $snap | % {
  72.                 $key = $_.vm.Name + "&" + ($_.Created.ToString())
  73.                 if($report.ContainsKey($key)){
  74.                         $_ | Add-Member -MemberType NoteProperty -Name Creator -Value $report[$key].User
  75.                 }
  76.                 $_
  77.         }
  78.         $snapshotsExtra
  79. }
  80.  
  81. Function SnapMail ($Mailto, $snapshot)
  82. {
  83.         $msg = new-object Net.Mail.MailMessage
  84.         $smtp = new-object Net.Mail.SmtpClient($smtpServer)
  85.         $msg.From = $MailFrom
  86.         $msg.To.Add($Mailto)
  87.  
  88.         $msg.Subject = "Snapshot Reminder"
  89.  
  90. $MailText = @"
  91. This is a reminder that you have a snapshot active on $($snapshot.VM) which was taken on $($snapshot.Created).
  92.  
  93. Name: $($snapshot.Name)
  94.  
  95. Description: $($snapshot.Description)
  96. "@     
  97.  
  98.         $msg.Body = $MailText
  99.         $smtp.Send($msg)
  100. }
  101.  
  102. Connect-VIServer $VISRV
  103.  
  104. foreach ($snap in (Get-VM | Get-Snapshot | Where {$_.Created -lt ((Get-Date).AddDays(-14))})){
  105.         $SnapshotInfo = Get-SnapshotExtra $snap
  106.         $mailto = ((Find-User $SnapshotInfo.Creator).Properties.mail)
  107.         SnapMail $mailto $SnapshotInfo
  108. }

Submit a correction or amendment below (
click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:


Remember me