1 2 3 4 Previous Next 46 Replies Latest reply: May 14, 2015 12:20 PM by LucD Go to original post RSS
      • 15. Re: Who created the Snapshot
        AlbertWT Master

        Wow, this is so cool guys an all star thread :-)

        thanks guys.

        • 16. Re: Who created the Snapshot
          BKeadle Novice

          It would seem all/any snapshots the owner is unknown.   Any idea why this would be?

          • 17. Re: Who created the Snapshot
            LucD Guru User Moderators vExpert

            The script tries to macth the Event, which holds the creator information, with a snapshot based on the timestamp availble on the snapshot and in the event.

            I noticed that sometimes these timestamps might differ with a couple of seconds.

            A solution would be to not look for an exact match of the timestamp, but look for an event that is "close enough". For example a couple of seconds before and after the snapshot create time.

            This poses a certain risk of matching the wrong event with the snapshot, but that can be avoided by adding an additional test, for example for the VM name. It's highly unlikely that you have 2 snapshots for the same VM in a matter of seconds.

            • 18. Re: Who created the Snapshot
              BKeadle Novice

              Agreed - but *how* to do this?   Looking through the script, I see this seemingly relevant function:

              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
              }

              Isn't this already adjusting by 5 seconds?  This still seems like it's testing for a specific time.  Is there a better way to do this so that I can find an entry, say +/- 10 seconds?

              • 19. Re: Who created the Snapshot
                LucD Guru User Moderators vExpert

                It reads the events starting 5 seconds before the create time it gets from the snapshot.

                But later on it calculates a key, containing the time from the event, for the hashtable

                $key = $_.EntityName + "&" + ($snapshot.CreateTime.ToLocalTime().ToString())

                which it then later on uses the time from the snapshot to create a new key which it uses to check if there is an antry with that key in the hash table.

                if($report.ContainsKey($key)){

                What you could do is round the time in the key to for example a 10 seconds interval.

                In other words, these time values, 21:17:10/21:17:14/21:17:19, would all become 21:17:10.

                This is still not a 100% foolproof solution but should produce better matches between the create time from the snapshot and the event time.

                 

                PS: you could of course allso end up with "unknow" creators because that property is simply not there in the event.

                I have seen that happen also a couple of times.

                • 20. Re: Who created the Snapshot
                  BKeadle Novice

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

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

                  • 21. Re: Who created the Snapshot
                    BKeadle Novice

                    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!

                    • 22. Re: Who created the Snapshot
                      LucD Guru User Moderators vExpert

                      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.

                      • 23. Re: Who created the Snapshot
                        BKeadle Novice

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

                        • 24. Re: Who created the Snapshot
                          dionndt Novice

                          Great thread!

                           

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

                          • 25. Re: Who created the Snapshot
                            LucD Guru User Moderators vExpert

                            I think you better ask that in the CLI Community.

                            William probably already has a Perl script for that

                            • 26. Re: Who created the Snapshot
                              monderick Enthusiast

                              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

                              • 27. Re: Who created the Snapshot
                                LucD Guru User Moderators vExpert

                                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 ?

                                • 28. Re: Who created the Snapshot
                                  monderick Enthusiast

                                  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

                                  • 29. Re: Who created the Snapshot
                                    LucD Guru User Moderators vExpert

                                    It looks as if the $snapshot variable contains $null.

                                    Are you using Alan's version of the script (2nd post in this thread) ?