PowerCLI

 View Only
Expand all | Collapse all

Snapshots report

  • 1.  Snapshots report

    Posted Jul 18, 2017 10:24 AM

    Hello, I would like to run a script to get snapshots list (csv) with: Who created (user) When (date) Snapshot name and description, Host and VM name., Space in Gb or Mb, Datacenter/Cluster I use RvTools  but with this tool I can't get who created. Thanks



  • 2.  RE: Snapshots report

    Posted Jul 18, 2017 10:33 AM

    Have a look on this one Snapshot Management – Who done and When #PowerShell #VMware | CheckYourLogs.Net 

    This should cover all your queries.

    Please consider marking this answer "correct" or "helpful" if you think your query have been answered correctly.



  • 3.  RE: Snapshots report

    Posted Jul 18, 2017 10:44 AM

    Do you actually ever read a question completely?

    And please, please stop begging for points :smileysad:



  • 4.  RE: Snapshots report

    Posted Jul 18, 2017 11:08 AM

    I don't know what is the problem here. I read question and somewhere it's response relevant to the query. Someone will give point here only if his/her query has been answered or helpful.



  • 5.  RE: Snapshots report

    Posted Jul 18, 2017 11:41 AM

    Guys ,I really appreciate yours help, always I can see yours answers and I solved most of my tasks, please don't fight for me :-) vijayrana968 Snapshots Management is quite complex, I would like to run just a script if is possible to get that information's because I need just sometimes to check if there are very old snapshots where some colleague 'forgot' to consolidate/delete and maybe name and description are not clear to understand which change or task refer. Thanks :-)



  • 6.  RE: Snapshots report

    Posted Jul 18, 2017 03:08 PM

    I am beginner to scripting, don't know its in deep but I try to make some tweaks to get what I want. Goal to being here is gain and share knowledge, not for points.

    I used below script, getting most of the things you need except VMHost. I am still checking this further.

    $Report = Get-VM |

        Get-Snapshot |

        Select VM,

        Name,

        Description,

        @{Name="User"; Expression = { (Get-VIEvent -Entity $_.VM -Start $_.Created.AddSeconds(-10) | Where {$_.Info.DescriptionId -eq "VirtualMachine.createSnapshot"} | Sort-Object CreatedTime | Select-Object -First 1).UserName}},

        @{Name="SizeGB";Expression={ [math]::Round($_.SizeGB,2) }},

        Created,

        @{Name="Days Old";Expression={ (New-TimeSpan -End (Get-Date) -Start $_.Created).Days }} | Export-Csv C:\Reports\Snapshotreport.csv

    The results are like this..



  • 7.  RE: Snapshots report

    Posted Jul 19, 2017 02:26 AM

    Function Get-SnapshotCreator {

        Param (

            [string]$VM,

            [datetime]$Created

        )

        (Get-VIEvent -Entity $VM -Types Info -Start $Created.AddSeconds(-10) -Finish $Created.AddSeconds(10) | Where-Object {$_.FullFormattedMessage  -eq 'Task: VirtualMachine.createSnapshot'} | Select -ExpandProperty UserName).Split("\")[-1]

    }

    $csv =  import-csv  <Location>

    foreach($vm in $csv){

        Get-Cluster |Get-VM $vm | Get-Snapshot | select  VM,`

        @{Name="SnapShotName";Expression={ $_.Name}},

        @{Name="CreatedBy";Expression={ Get-SnapshotCreator -VM $_.VM -Created $_.Created }},

        PowerState,

        @{Name="SnapShot Description";Expression={ $_.Description}}, 

        @{Name="SizeGB";Expression={ [math]::Round($_.SizeGB,2) }},

        Created,

        @{Name="Cluster";Expression={ Get-Cluster -VM $_.VM | select -ExpandProperty name }}

    }



  • 8.  RE: Snapshots report

    Posted Jul 19, 2017 08:20 AM

    One more line can be added for hosts as anoton need Host infor as well.

    @{Name="Host";Expression={ Get-VMHost -VM $_.VM | select -ExpandProperty name }},



  • 9.  RE: Snapshots report

    Posted Jul 19, 2017 09:11 AM

    Try like this.

    You might want to play with 1 minute, before and after, interval in the Get-VIEvent cmdlet.

    Depending how busy your vCenter is, there might be a bigger delta between the snapshot timestamp and the event.

    Update: fixed an issue with double User names.

    This would happen when a snapshot was taken, then deleted and a new snapshot taken, within the time span of 2 minutes

    $clusterName = 'MyCluster' 

     

    Get-Cluster -Name $clusterName | Get-VM | Get-Snapshot |

    select  Name,

        Created,

        @{N='VM';E={$_.VM.Name}},

        @{N='VMHost';E={$_.VM.VMHost.Name}},

        @{N='Cluster';E={$_.VM.VMHost.Parent.Name}},

        @{N='Datacenter';E={

            $p = Get-View -Id $_.VM.VMHost.Parent.ExtensionData.Parent -Property Name,Parent

            while($p -and $p -isnot [VMware.Vim.Datacenter]){

                $p = Get-View -Id $p.Parent -Property Name,Parent

            }

            if($p){

                $p.Name

            }

        }},

        @{N='SizeMB';E={[math]::Round($_.SizeMB,1)}}, 

        @{N='SizeGB';E={[math]::Round($_.SizeGB,1)}},

        @{N='User';E={

            Get-VIEvent -Start $_.Created.AddMinutes(-1) -Finish $_.Created.AddMinutes(1) |

            where{$_ -is [VMware.Vim.TaskEvent] -and $_.Info.DescriptionId -eq 'VirtualMachine.createSnapshot'} |

            select -Last 1 -ExpandProperty UserName

        }} |

    Export-Csv snap-report.csv -NoTypeInformation -UseCulture



  • 10.  RE: Snapshots report

    Posted May 30, 2018 05:07 AM

    Hi Luc,

    is there anyway that this script could determine snapshot creator if it was awhile back? perhaps like 2-3 months old?

    And how this script could call not just a single vcenter but many vcenters to look for snapshot?

    Thanks.



  • 11.  RE: Snapshots report

    Posted May 30, 2018 05:13 AM

    If you keep the events in the vCenter for that long, the User field should show the creator of the snapshot.

    You can connect to all vCenters, and then use a ForEach loop of all vCenters in $global:defaultVIServers.

    And add the Server paramater on the other cmdlets.



  • 12.  RE: Snapshots report

    Posted Feb 20, 2024 01:59 PM

    This one work great, thank you for sharing!



  • 13.  RE: Snapshots report

    Posted Jul 24, 2017 08:52 AM

    Well, I using this one:

    it gave me user ad execution is fast,

    Get-Snapshot |

        Select VM,

        Name,

        Description,

        @{Name="User"; Expression = { (Get-VIEvent -Entity $_.VM -Start $_.Created.AddSeconds(-10) | Where {$_.Info.DescriptionId -eq "VirtualMachine.createSnapshot"} | Sort-Object CreatedTime | Select-Object -First 1).UserName}},

        @{Name="SizeGB";Expression={ [math]::Round($_.SizeGB,2) }},

        Created,

        @{Name="Days Old";Expression={ (New-TimeSpan -End (Get-Date) -Start $_.Created).Days }} | Export-Csv C:\Users\gemela\Desktop\Snapshotreport.csv

    I will try others too and let you know.