VMware Cloud Community
alanrenouf
VMware Employee
VMware Employee
Jump to solution

Who created the Snapshot

Any idea how I can relatemy Get-Snapshot information to the Get-VIEvent information to work out who created a snapshot ?

Thanks

Alan

Blog: http://virtu-al.net Twitter: http://twitter.com/alanrenouf Co-author of the PowerCLI Book: http://powerclibook.com
Tags (1)
Reply
0 Kudos
76 Replies
BKeadle
Contributor
Contributor
Jump to solution

Sounds good.  How do I go about rounding the time in the key then?

(thanks so much for your knowledge and willingness to help!)

Reply
0 Kudos
BKeadle
Contributor
Contributor
Jump to solution

As shown here:

vsnapshot.jpg

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!

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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

Reply
0 Kudos
BKeadle
Contributor
Contributor
Jump to solution

Interesting to note - that my vCenter is an outlier - something for me to investigate.

Reply
0 Kudos
dionndt
Contributor
Contributor
Jump to solution

Great thread!

Anyone know how difficult it would be to convert this script to perl?

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

I think you better ask that in the CLI Community.

William probably already has a Perl script for that Smiley Wink


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
monderick
Enthusiast
Enthusiast
Jump to solution

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

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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

Reply
0 Kudos
monderick
Enthusiast
Enthusiast
Jump to solution

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

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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

Reply
0 Kudos
monderick
Enthusiast
Enthusiast
Jump to solution

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.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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

Reply
0 Kudos
monderick
Enthusiast
Enthusiast
Jump to solution

aye, snapshot was created today.

Reply
0 Kudos
bseymour
Contributor
Contributor
Jump to solution

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?

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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

Reply
0 Kudos
bseymour
Contributor
Contributor
Jump to solution

# - 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

}

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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

Reply
0 Kudos
bseymour
Contributor
Contributor
Jump to solution

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?

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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

Reply
0 Kudos
bseymour
Contributor
Contributor
Jump to solution

No.

Reply
0 Kudos