I am having a hard time getting either SnapReminder or vCheck to find the Snapshot Creator info.
vCheck finds all the other information about the snapshot, just not the Snapshot Creator.
With SnapReminder, I have "modified" the script slightly, so that it always sends the email to my personal email
and so that it includes the info about the Snapshot Creator in the $MailText variable.
Here is my "modified" SnapReminder script:
--------------------------------------------------------------------------------------------------------
# - SnapReminder V1.0 By Virtu-Al - http://virtu-al.net
#
# Please use the below variables to define your settings before use
#
$smtpServer = "smtp.mycompany.com"
$MailFrom = "job@mycompany.com"
$VISRV = "vcenter01.mycompany.com"
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)
Creator: $($snapshot.Creator)
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 = "my-email@mycompany.com"
SnapMail $mailto $SnapshotInfo
}
--------------------------------------------------------------------------------------------------------
When I run the SnapReminder script I get a bunch of errors in the PowerCLI window:
Errors shown below:
--------------------------------------------------------------------------------------------------------
You cannot call a method on a null-valued expression.
At C:\Users\myusername\Desktop\find-snapshot-creator.ps1:63 char:4
+ $key = $_.EntityName + "&" + ($snapshot.CreateTime.ToString())
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
Index operation failed; the array index evaluated to null.
At C:\Users\myusername\Desktop\find-snapshot-creator.ps1:64 char:4
+ $report[$key] = $row
+ ~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : NullArrayIndex
You cannot call a method on a null-valued expression.
At C:\Users\myusername\Desktop\find-snapshot-creator.ps1:63 char:4
+ $key = $_.EntityName + "&" + ($snapshot.CreateTime.ToString())
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
Index operation failed; the array index evaluated to null.
At C:\Users\myusername\Desktop\find-snapshot-creator.ps1:64 char:4
+ $report[$key] = $row
+ ~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : NullArrayIndex
You cannot call a method on a null-valued expression.
At C:\Users\myusername\Desktop\find-snapshot-creator.ps1:63 char:4
+ $key = $_.EntityName + "&" + ($snapshot.CreateTime.ToString())
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
Index operation failed; the array index evaluated to null.
At C:\Users\myusername\Desktop\find-snapshot-creator.ps1:64 char:4
+ $report[$key] = $row
+ ~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : NullArrayIndex
You cannot call a method on a null-valued expression.
At C:\Users\myusername\Desktop\find-snapshot-creator.ps1:63 char:4
+ $key = $_.EntityName + "&" + ($snapshot.CreateTime.ToString())
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
You cannot call a method on a null-valued expression.
At C:\Users\myusername\Desktop\find-snapshot-creator.ps1:63 char:4
+ $key = $_.EntityName + "&" + ($snapshot.CreateTime.ToString())
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
You cannot call a method on a null-valued expression.
At C:\Users\myusername\Desktop\find-snapshot-creator.ps1:63 char:4
+ $key = $_.EntityName + "&" + ($snapshot.CreateTime.ToString())
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
You cannot call a method on a null-valued expression.
At C:\Users\myusername\Desktop\find-snapshot-creator.ps1:63 char:4
+ $key = $_.EntityName + "&" + ($snapshot.CreateTime.ToString())
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
You cannot call a method on a null-valued expression.
At C:\Users\myusername\Desktop\find-snapshot-creator.ps1:63 char:4
+ $key = $_.EntityName + "&" + ($snapshot.CreateTime.ToString())
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
You cannot call a method on a null-valued expression.
At C:\Users\myusername\Desktop\find-snapshot-creator.ps1:63 char:4
+ $key = $_.EntityName + "&" + ($snapshot.CreateTime.ToString())
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
You cannot call a method on a null-valued expression.
At C:\Users\myusername\Desktop\find-snapshot-creator.ps1:63 char:4
+ $key = $_.EntityName + "&" + ($snapshot.CreateTime.ToString())
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
You cannot call a method on a null-valued expression.
At C:\Users\myusername\Desktop\find-snapshot-creator.ps1:63 char:4
+ $key = $_.EntityName + "&" + ($snapshot.CreateTime.ToString())
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
--------------------------------------------------------------------------------------------------------
Currently I don't receive an email, when the script is run.
I did that yesterday (??), and in that email I received it was stated that the creator of the snapshot (I only have one snapshot currently, 15 days old) was our Veeam Serivce Account.
That is not correct, this particular snapshot is manually created by my colleague, which is also stated in the "Tasks" list for this particular VM when viewed in vSphere Client.
It should be said that the Veeam Service Account creates and deletes a snapshot each day for this VM, as part of our Veeam backup schedule.
Information about the snapshot:
Created Time reported by Get-Snapshot: 25-03-2014 19:32:35
Task reported in vSphere Client:
----------------------------------
Create virtual machine snapshot
VMName
Completed
MYCOMPANY\mycolleague
vcenter01.mycompany.com
25-03-2014 19:32:46
25-03-2014 19:32:46
25-03-2014 19:32:50
----------------------------------
Version Information about the environment:
ESXi Hosts: 5.0.0, 1489271 (ESXi 5.0 Patch 7)
vCenter: 5.0.0, 455964 (vCenter Server 5.0 GA)
PowerCLI: 5.5 Release 1 build 1295336
PowerShell: 4.0
OS Information about the environment:
vCenter: Windows Server 2008 R2 Datacenter (64bit)
Computer the script is run from: Windows Server 2012 R2 Datacenter (64bit)
Anyone that can explain what is going on, and maybe help me to get this to work?
Thanks
Looks like you might have 1 or more VMs with (a) snapshot(s), and that some of these snapshots don't seem to have the Created property filled in correctly.
Can you check with
Get-VM | Get-Snapshot | Select @{N="VM";E={$_.VM.Name}},Name,Description,Created
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Here is the output of Get-VM | Get-Snapshot | Select @{N="VM";E={$_.VM.Name}},Name,Description,Created
VM Name Description Created
-- ---- ----------- -------
DEVTS01 beforeTLA3partro... beforeTLA3partro... 25-03-2014 19:32:35
RDSPKOL41 before WSUS 10-04-2014 09:30:09
Does not look wrong to me…?