scripts/PowerShell/get_snapshots.ps1

55 lines
1.7 KiB
PowerShell
Raw Permalink Normal View History

2022-02-07 16:29:26 +01:00
#!/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!"
}