PowerShell | PowerCLI Snapshots
When managing server patching, we always add a snapshot to the server before patching at let it stay there for about 3-4 days before removal to assure that we don’t need to roll back. These are the scripts I use for creation, review, and removal. Scripts assume you are already connected to your vCenter server.
​
​
Get list of computers and check for snapshots by VM name
$Computers=Get-VM | Select -ExpandProperty Name
Foreach($Comp in $Computers){Get-VM $Comp | Get-Snapshot | select VM, Name, Created, SizeGB,description | Export-CSV C:\temp\All-Snaps-Review.csv -NoTypeInformation -Append
}
Create snapshot on multiple servers with 10 second interval, sources from file.
$VMs=Get-Content C:\temp\Snaps\Snapper.txt
#Loop Through Updated List and Create Snaps before Patch Window
Foreach($VM in $VMs){
Get-VM $VM | New-Snapshot -name "Pre-Patch Snap" -Description "Script Created-Prior to Patching" -RunAsync -ErrorAction Stop
Sleep 10
}
​
Import list of computers, delete specific snapshot by name with 30 second interval
Suggest only having like 6 or less systems in source file as to not stress storage system.
SQL and File servers seem to take the longest, just an FYI.
$Computers = Get-Content C:\temp\Snaps\One.txt
Foreach($Comp in $Computers){
Get-Snapshot -VM $Comp | Where{$_.Name -eq 'Pre-Patch Snap'} | Remove-Snapshot -RunAsync -Confirm:$False
sleep 30
}
PowerShell | PowerCLI Stop-VM
I only had to use once, when I had data corruption at the LUN level. The VM would lock up during the attempt to remove a snapshot from a Veeam backup. To use optional "-Kill" parameter, you need to have a direct connection to the ESXi host.
​
$VMHosts=Read-Host "Enter VMHost"
Get-VMHost $VMHosts | Get-VM
$VMKILL=Read-Host "Enter VM Name"
Stop-VM -Kill $VMKILL -Confirm:$false
PowerCLI Check for Secure Boot Option
#Imports PowerCLI VMware PS Module
Import-Module VMware.VimAutomation.core
#Had to set this to connect to server
#Set-PowerCLIConfiguration -InvalidCertificateAction Ignore
#Connects to vCenter
Connect-VIServer VCENTERServerName
$vms=Get-VM | Where{$_.GuestID -like "*Windows2019*"}
Foreach($vm in $vms){
$vm.Name
$vm.extensiondata.config.bootoptions.efisecurebootenabled
}