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!" +}