VMware Cloud Community
antoniogemelli
Hot Shot
Hot Shot

Snapshots report

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

12 Replies
vijayrana968
Virtuoso
Virtuoso

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.

0 Kudos
LucD
Leadership
Leadership

Do you actually ever read a question completely?

And please, please stop begging for points Smiley Sad


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

vijayrana968
Virtuoso
Virtuoso

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.

0 Kudos
antoniogemelli
Hot Shot
Hot Shot

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 🙂

0 Kudos
vijayrana968
Virtuoso
Virtuoso

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

pastedImage_1.png

jonathanmsa
Contributor
Contributor

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

}

0 Kudos
vijayrana968
Virtuoso
Virtuoso

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 }},

0 Kudos
LucD
Leadership
Leadership

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


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

antoniogemelli
Hot Shot
Hot Shot

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.

0 Kudos
afhamsani
Enthusiast
Enthusiast

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.

0 Kudos
LucD
Leadership
Leadership

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.


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

0 Kudos
GAlexandres
Contributor
Contributor

This one work great, thank you for sharing!

0 Kudos