From 1b69f27660b50637e2e96a5b85bdef2c8ef36b73 Mon Sep 17 00:00:00 2001 From: Simon Cornet Date: Mon, 7 Feb 2022 16:29:26 +0100 Subject: [PATCH] [get_snapshots] Added script --- PowerShell/get_snapshots.ps1 | 54 ++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 PowerShell/get_snapshots.ps1 diff --git a/PowerShell/get_snapshots.ps1 b/PowerShell/get_snapshots.ps1 new file mode 100644 index 0000000..2fbe716 --- /dev/null +++ b/PowerShell/get_snapshots.ps1 @@ -0,0 +1,54 @@ +#!/snap/bin/pwsh + +# Prerequisites +Set-PowerCLIConfiguration -Scope User ` + -ParticipateInCeip $false ` + -confirm:$false | out-null + +# Config section +$vcenter = "vcenter_server" +$user = "vcenter_user" +$password = "vcenter_user_password" + +# Connect to vCenter +Set-PowerCLIConfiguration -InvalidCertificateAction ignore ` + -confirm:$false | out-null + +Connect-VIServer -Server $vcenter ` + -User $user ` + -Password $password | out-null + +# Get snapshots +$results = @() +$vmsnapshots = Get-VM | ` + Where {$_.Name -notmatch ".*replica.*" } | ` + Get-Snapshot + +if ($vmsnapshots.Name -ne $null) { + foreach ($snapshot in $vmsnapshots) { + $snapevent = Get-VIEvent -Entity $snapshot.VM ` + -Types Info ` + -Finish $snapshot.Created ` + -MaxSamples 1 | ` + Where-Object {$_.FullFormattedMessage -imatch 'Task: Create virtual machine snapshot'} + + if ($snapevent -ne $null) { + $user = [string]$snapevent.UserName + $snapshot | Add-Member CreatedBy $user + } + else { + $snapshot | Add-Member CreatedBy '-- Unknown --' + } + $results = $results + $snapshot + } + + $results = $results | Sort-Object -Property Created + $results | Format-Table -Property VM, ` + CreatedBy, ` + @{Label="CreatedOn"; Expression={Get-Date $_.Created -Format "dd.MM.yyyy"}}, ` + Name + +} +else { + Write-Output " Congratulations, there are no snapshots!" +}