-->

Using PowerShell to Remove Virtual Machine Snapshots in VMware

This article describes how to remove snapshots via PowerShell.


What is the Snapshot?

VMware Snapshot preserves the state and data of a virtual machine at a specific point in time.

Snapshot state includes the virtual machine’s power state (ex, powered-on, powered-off, suspended) and the data includes all of the files (disks, memory, and other devices, such as virtual network interface cards.) that make up the virtual machine.

This point in time is very important, since it is what you need to keep in mind when it comes time to either delete your snapshot (keep your changes) or revert your snapshot (discard your changes).

How to remove snapshots in vCenter by using powershell.

 1. Open Powershell ISE as an administrator account

 2.  Note down the below script as a ps1 format.

 3. Open the script in which we saved the Ps1 file and run it.

 4. Please find the below screenshots for reference.




Note:  ($_.Name -match "patch" ) Please change the word "patch" if need. And you can modify the days.

How many old days you want to be deleted. we have prepared the script which is 3 days old and relevant patch snapshots.

----------------------------------------------------------------------------------


try {

Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Unrestricted -Force

Import-Module VMware.VimAutomation.Core

[Net.ServicePointManager]::SecurityProtocol = 

[Net.SecurityProtocolType]::Tls12


Connect-VIServer 'XXXXX' -user 'XXXX' -password 'XXXXX' -ErrorAction Stop -WarningAction 0

}

catch {  

        Write-Host "Unable to connect vcenter server due to exception" $_

exit

}

try {

#Gets the list of snapshots created and saves in $vms.

$vms=Get-VM | Get-SnapShot | Select vm,@{n='SnapShotName';e={$_.name}},@{n='SizeInGB'; e={[math]::Round($_.sizeGB,3)}},created

#Saves VM's list in file which are having snapshots

$l = $vms | select VM | Out-File 'C:\Windows\Temp\vmsnapsh.txt'

#Prints the list of snapshots

    if ($vms -eq $null) {

        write-host "No Snapshots found"

        exit

        }

    else

        {

write-host "List of Snapshots:"

        $vms

        }

#Gets the content in file and saves in $k

$k=(Get-Content -Path 'C:\Windows\Temp\vmsnapshotlist.txt')

$c=0

for ($i=3; $i -lt ($k.Length-2); $i++)

{

#Checks Snapshot of each VM if it is having snapshot with name Patching and if it is older than 5 days.

    $delVms = get-vm -Name $k[$i].trim() | get-snapshot | where {($_.Name -match "patch") -and (((Get-Date)-($_.created)).days -ge 3)} 

        if ($delVms -ne $null){

            Write-Host "Deleted VM snapshot on" $delVms.vm

#Deletes the snapshot with name patching and older than 3 days

            $delVms | Remove-Snapshot -confirm:$false

            $c++

        }

    }

if ($c -eq 0)

{

    Write-Host "No Snapshots to delete which are older than 3 days"

}

}

catch {

Write-Host "Unable to delete snapshot due to exception" $_

}


----------------------------------------------***----------------------------------------------
Using PowerShell to Remove Virtual Machine Snapshots in VMware Using PowerShell to Remove Virtual Machine Snapshots in VMware Reviewed by Virtulization on April 22, 2021 Rating: 5

No comments:

Home - PageNavi (show/hide)

hide
Powered by Blogger.