Any idea how I can relatemy Get-Snapshot information to the Get-VIEvent information to work out who created a snapshot ?
Thanks
Alan
Sounds good. How do I go about rounding the time in the key then?
(thanks so much for your knowledge and willingness to help!)
As shown here:
The snapshot event/tasks on the VM shows created at 12:51:35 PM (by VOP\JHerold) and the snapshot reminder shows created at 12:52:18 PM but Unknown Creator. Shouldn't the code have picked this up?
UPDATE:
Since the difference in time was 43 seconds, I simply changed -5 to -60 (within 1 minute) and it now identifies the Creator owner - yay!
On a busy vCenter the timestamp difference between the action (snapshot in this case) and the corresponding event timestamp can be relatively big. But this is the first time I see such a huge difference.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Interesting to note - that my vCenter is an outlier - something for me to investigate.
Great thread!
Anyone know how difficult it would be to convert this script to perl?
I think you better ask that in the CLI Community.
William probably already has a Perl script for that
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
is this script still functional on powerCLI 5.0.1? connecting to our 4.1 U2 vCenter servers produces the below errors.
thanks
Method invocation failed because [System.Object[]] doesn't contain a method named 'CreateCollectorForTasks'.
At test.ps1:37 char:66
+ $collectionImpl = Get-View ($taskMgr.CreateCollectorForTasks <<<< ($filter))
+ CategoryInfo : InvalidOperation: (CreateCollectorForTasks:String) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound
You cannot call a method on a null-valued expression.
At test.ps1:40 char:49
+ $collection = $collectionImpl.ReadNextTasks <<<< ($tasknumber)
+ CategoryInfo : InvalidOperation: (ReadNextTasks:String) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
You cannot call a method on a null-valued expression.
At test.ps1:52 char:38
+ $collectionImpl.DestroyCollector <<<< ()
+ CategoryInfo : InvalidOperation: (DestroyCollector:String) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
It looks as if you're connected to multiple vSPhere servers. (check by displaying $defaultVIServers)
In that case Get-View TaskManager will return an array of TaskManager objects.
Can you try while connected to 1 vCenter ?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
thanks LucD, i tested while only connected to one VC and the errors changed a bit
You cannot call a method on a null-valued expression.
At c:\test.ps1:44 char:62
+ $key = $_.EntityName + "&" + ($snapshot.CreateTime.ToString <<<< ())
+ CategoryInfo : InvalidOperation: (ToString:String) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
Index operation failed; the array index evaluated to null.
At c:\test.ps1:45 char:11
+ $report[ <<<< $key] = $row
+ CategoryInfo : InvalidOperation: (System.Collections.Hashtable:Hashtable) [], RuntimeException
+ FullyQualifiedErrorId : NullArrayIndex
It looks as if the $snapshot variable contains $null.
Are you using Alan's version of the script (2nd post in this thread) ?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Afraid I get the same result on both versions.
If i run a simple 'get-vm | get-snapshot' from the same powerCLI window, I get results returned.
also tried from a powerCLI session on one of the vCenter servers.
The problem you seem to be having is not with the Get-SNapshot cmdlet, but with the ability to find the event that corresponds with the creation of the snapshot.
The function only looks at the events from the last 5 days.
Are you testing with a VM that only has snapshots created during the last 5 days ?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
aye, snapshot was created today.
I run this script but it just comes back showing nothing. I have snapshots that I created but I get no email.Is there seomthign that needs to be changed for 5.0?
The original script writes the results to a CSV file, there is no mail being sent.
Which script are you using ?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
# - SnapReminder V1.0 By Virtu-Al - http://virtu-al.net
#
# Please use the below variables to define your settings before use
#
$smtpServer = "SMTPServerName"
$MailFrom = "EmailAddress"
$VISRV = "vCernterServer"
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"
$filter.State = "success"
$filter.Entity = New-Object VMware.Vim.TaskFilterSpecByEntity
$filter.Entity.recursion = "self"
$filter.Entity.entity = (Get-Vm -Name $snap.VM.Name).Extensiondata.MoRef
$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
if ( $snapshot -ne $null)
{
$key = $_.EntityName + "&" + ($snapshot.CreateTime.ToLocalTime().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.ToLocalTime().ToString())
if($report.ContainsKey($key)){
$_ | Add-Member -MemberType NoteProperty -Name Creator -Value $report[$key].User
write-host $report[$key].User is creator of $key
}
$_
}
$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
}
It looks as you're missing the Find-User function. Did you get the script from SnapReminder ?
Also note that this script will send an email to the users that created the snapshots, not necessarily the person who is running the script
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
I got this from the SnapReminder you referenced. I created a bunch of snapshots with my username so although Iam running the script it should find and email me about mine. What part is missing?
Did you create those snapshots longer than 14 days ago ?
The script checks if the snapshot's creation date is more than 14 days ago.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
No.