Skip to content

Instantly share code, notes, and snippets.

@phyoewaipaing
Last active November 12, 2023 13:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phyoewaipaing/87527080d846619c7bb2f44d9cbafea1 to your computer and use it in GitHub Desktop.
Save phyoewaipaing/87527080d846619c7bb2f44d9cbafea1 to your computer and use it in GitHub Desktop.
List VM Snapshot with Last Boot Time
<#
.SYNOPSIS
Script that will list VMs with Snapshots with the Last VM Reboot Time Info.
.DESCRIPTION
Script that will list VMs with Snapshots with the Last VM Reboot Time Info.
It can be used to compare whether the last VM reboot has been performed after the specified
snapshot, useful for windows update process or other configuration changes which are needed to be
inspected after the VM reboot. In some cases, you might need to find which snapshots can be deleted
after the successful VM reboot and this script is what you need. Cheers !
Note: You need to connect vCenter Server or Esxi Hosts before running this script.
Column Explanation:
VM - Name of the VM which has snapshot(s).
SnapshotName - Name of the snapshot
Description - Description of the snapshot
Created - The date/time snapshot is created
VmBootTime - The time when VM is powered on
B4_Reboot - 'Yes' if the snapshot is taken before the VM is powered on
'No' if the snapshot is taken after the VM is powered on
SizeGB - Size of the Snapshot in GB
VmPowerState - Powerstate of the Virtual Machine
Example usage:
.\Get_VM_Snapshot_Reboot_Info.ps1
Author: Phyoe Wai Paing
Country: Myanmar(Burma)
Released: 02/12/2018
.EXAMPLE
.\Get_VM_Snapshot_Reboot_Info.ps1
This will list VMs with snapshots with last reboot time information.
.LINK
You can find this script and more at: https://www.scriptinghouse.com/
#>
### Expression to calculate the VM Powered on Date from the uptime ###
$LastBootProp = @{
Name = 'LastBootTime';
Expression = {
If ($_.Summary.QuickStats.UptimeSeconds -eq 0 -OR !($_.Summary.QuickStats.UptimeSeconds))
{ "N/A" }
else
{ ( Get-Date ) - ( New-TimeSpan -Seconds $_.Summary.QuickStats.UptimeSeconds ) }
}
}
### Get-View to find VM with snapshots and the uptime info ###
$VmBootTime = Get-View -ViewType VirtualMachine -Filter @{"snapshot" = ""} -Property Name,Summary.QuickStats.UptimeSeconds |
Select Name,$LastBootProp
### Instead of digging the snapshot properties from Get-View, we will use Get-Snapshot ###
$VmSnapshot = Get-VM ($VmBootTime.Name) | % { $_ | Get-Snapshot | select VM,Name,Description,Created,@{N='SizeGB';Exp={ [math]::Round($_.SizeGB,2) }} | Add-Member "NoteProperty" "VmPowerState" $_.powerstate -PassThru }
$VmSnapshotBeforeCompare = $VmSnapshot | select VM,Name,Description,Created,@{N='VmBootTime';Exp={ $CurVM = $_.VM ; $VmBootTime | % { If($_.Name -eq $CurVM) { $_.LastBootTime } }}} , SizeGB, VmPowerState
### Put another line to compare the Snapshot Creation Datetime with VM Last Reboot Datetime from the previous output ###
$VmSnapshotBeforeCompare | select VM,@{N='SnapshotName';Exp={$_.Name}},Description,Created,VmBootTime,@{N='B4_Reboot';Exp= { Try { If( $_.Created -lt $_.VmBootTime ) {"Yes"} else {"No"} } catch { "N/A" } }}, SizeGB, VmPowerState
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment