I was wondering if there was a way to query vcenter with a powershell script to get a report on how often snapshots are taken and how often snapshots are reverted.
Thanks
Don't mention "manual" in the Automation Tools communities :smileygrin:
Try like this
$vms = Get-VM
$start = (Get-Date).AddDays(-1)
$snapTaken = Get-VIEventPlus -Entity $vms -EventType "TaskEvent" -Start $start |
where {$_.Info.DescriptionId -eq "VirtualMachine.createSnapshot"}
$snapRevert = Get-VIEventPlus -Entity $vms -EventType "com.vmware.vc.vm.VmStateRevertedToSnapshot" -Start $start
It should produce 2 arrays, one holding the snapshots that were taken and the snapshot reverts
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hello and welcome to the communities.
Note: Discussion successfully moved from VMware vCenter™ to VMware vSphere™ PowerCLI
You can get the snapshots taken and the snapshot reverts with the following script
$start = (Get-Date).AddMinutes(-30)
Get-VIEvent -MaxSamples ([int]::MaxValue) -Start $start |
where {($_ -is "VMware.Vim.TaskEvent"-and "VirtualMachine.createSnapshot" -eq $_.Info.DescriptionId) -or
($_ -is "VMware.Vim.EventEx"-and "com.vmware.vc.vm.VmStateRevertedToSnapshot" -eq $_.EventTypeId)} |
Select CreatedTime,UserName,@{N="VM";E={$_.Vm.Name}},FullFormattedMessage
But, unfortunately the TaskEvent that is created doesn't contain the name of the snapshot.
So part of the equation is missing I'm afraid.
You can still produce a total for the snapshots taken and a total for the snapshot reverts.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
I will give it a go. I am only interested in some statistics to see how often snaps are being done.
Thanks
The data is what I needed but I have 1 question. The script works fine with 1 of my vcenters that has about 150 VM's but on another vcenter which has 700 I get the following error if I change the addminutes to anything more than a day.
Get-VIEvent | The operation has timed out |
At :line:10 char:11
+ Get-VIEvent <<<< -MaxSamples ([int]::MaxValue) -Start $start | where {($_ -is "VMware.Vim.TaskEvent"-and "VirtualMachine.createSnapshot" -eq $_.Info.DescriptionId)} | Select CreatedTime,UserName,@{N="VM";E={$_.Vm.Name}},FullFormattedMessage | export-csv c:\Scripts\snaps.csv
I could run the report for a day at a time but that might take a while since I need 30 days.
Any help is greatly appreciated
It could be that there are a huge amount of events on that 2nd vCenter compared to the 1st vCenter.
Or the 2nd vCenter might be very busy.
As an alternative you could try to use the Get-VIEventPlus function from my Get the vMotion/svMotion history post.
There you can specify which type of events you want to retrieve and the filtering happens on the vCenter, so significantly less objects to be retrieved.
You probably better do 2 calls, one for the VirtualMachine.createSnapshot and one for the com.vmware.vc.vm.VmStateRevertedToSnapshot.
Let me know if you succeed in that ?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Copied the function over to a script and connect it to my vcenter and am using the following:
Connect-VIServer vcenter01
The VIEventPlus Function )
My Command
Get-VIEventPlus -Entity (Get-VM *) -EventType "VirtualMachine.createSnapshot" -Days 1 |Export-Csv C:\Scripts\snap-report1.csv -NoTypeINformation -UseCulture
I am receiving no results back by using this method. Is my command not formed properly?
Thanks again
The Get-VIEventPlus function doesn't have a Days parameter, that is the 2nd function, called Get-MotionHistory.
Use the Start parameter.
Try it like this
$vms = Get-VM
$start = (Get-Date).AddDays(-1)
Get-VIEventPlus -Entity $vms -EventType "VirtualMachine.createSnapshot" -Start $start
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Sorry about that.
I made the changes but no data is return still with using the Get-VIEventPlus function . Anything else to try before I suck it up and do it manually?
Thanks!
Don't mention "manual" in the Automation Tools communities :smileygrin:
Try like this
$vms = Get-VM
$start = (Get-Date).AddDays(-1)
$snapTaken = Get-VIEventPlus -Entity $vms -EventType "TaskEvent" -Start $start |
where {$_.Info.DescriptionId -eq "VirtualMachine.createSnapshot"}
$snapRevert = Get-VIEventPlus -Entity $vms -EventType "com.vmware.vc.vm.VmStateRevertedToSnapshot" -Start $start
It should produce 2 arrays, one holding the snapshots that were taken and the snapshot reverts
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
No results returned using the latest iteration either. Is there a way to do some debugging to check why no results are returning?
Yes, we can.
Do you have a GUI with debugging feature available ?
Something like PowerShell Plus or PowerGui ?
But first, can you check if your events and tasks are saved in the vCenter database ?
See here.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
The events and tasks are saved in the database according to the link.
I have Powershell Plus. Also, the original function works fine but times out if I
give it more than a few days to look up. So would that mean the events are
there?
When debugging it is cycling this code over and over for 10-15 minutes and comes back with no data.
$entity | %{
$eventFilter.entity.entity = $_.ExtensionData.MoRef
$eventCollector = Get-View ($eventMgr.CreateCollectorForEvents($eventFilter))
$eventsBuffer = $eventCollector.ReadNextEvents($eventnumber)
while($eventsBuffer){
$events += $eventsBuffer
$eventsBuffer = $eventCollector.ReadNextEvents($eventnumber)
}
$eventCollector.DestroyCollector()
DEBUG: 6/6/2013 12:17:40 PM Get-View
DEBUG: ! SET $eventCollector = 'VMware.Vim.EventHistoryCollector'.
DEBUG: 38+ $eventsBuffer = <<<< $eventCollector.ReadNextEvents($eventnumber)
DEBUG: ! SET $eventsBuffer = ''.
DEBUG: 39+ while <<<< ($eventsBuffer){
DEBUG: 43+ $eventCollector.DestroyCollector <<<< ()
DEBUG: 36+ $eventFilter.entity.entity = <<<<
$_.ExtensionData.MoRef
If that helps.
Just adding some more info. It appears that this is causing the issue?
Method invocation failed because [System.Object[]] doesn't contain a method named 'CreateCollectorForEvents'.
At C:\Users\xxxx\Documents\Power Shell Scripts\snaps created.ps1:37 char :69
+ $eventCollector = Get-View ($eventMgr.CreateCollectorForEvents <<
<< ($eventFilter))
+ CategoryInfo : InvalidOperation: (CreateCollectorForEvent
s:String) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound
That could mean there are a huge amount of events to process when you specify a couple of days.
And possibly the vCenter can't cope (server load, DB load...) or the client where you run the script can't cope.
Running the script over a longer period of time with the PSP debugger switched on will put a high load on the client where you run the script.
You should be able to see that when you open the Performance Manager on the client.
Did you try specifying a short time interval, but one where you are sure there was a snapshot and a revert ?
That will allow you to at least test if the functionality of the functions and the scripts work.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
For some reason even though I logged in with Chrome it wouldn't recognize my login on the forum.
I was able to get the reports I need by moving the script to a host with more memory and separating out the
create and revert to separate scripts.
Thanks for your assistance, it was greatly appreciated.