VMware Cloud Community
tdubb123
Expert
Expert
Jump to solution

VM snapshot delete and creation

trying to create a daily snapshopt and delete the one from previous day

$vms = get-content "c:\scripts\vms.txt"

$today = (get-date).tostring("MM-dd-yyy")

foreach ($vm in $vms) {

$snapshot = $vm | get-snapshot

if ($snapshot) {

$snapshot | remove-snapshot -confirm:$false

get-vm -name $vm | new-snapshot -name "$vm-($today)" -description "$today"

}

will this work?

1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

To avoid removing other snapshots, I suggest a small change.

This will only remove the snapshot created yesterday by this script (or at least following the naming convention in this script)

$vms = Get-Content "c:\scripts\vms.txt"

$now = Get-Date

$today = $now.ToString(("MM-dd-yyy"))

$yesterday = $now.AddDays(-1).ToString(("MM-dd-yyy"))

foreach ($vm in (Get-VM -Name $vms)) {

    $snapshot = $vm | Get-Snapshot -Name "$($vm.Name)-$($yesterday)"

    if ($snapshot) {

        $snapshot | Remove-Snapshot -confirm:$false

    ]

    New-Snapshot -VM $vm -Name "$($vm.Name)-$($today)" -Description "$today"

}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

2 Replies
LucD
Leadership
Leadership
Jump to solution

To avoid removing other snapshots, I suggest a small change.

This will only remove the snapshot created yesterday by this script (or at least following the naming convention in this script)

$vms = Get-Content "c:\scripts\vms.txt"

$now = Get-Date

$today = $now.ToString(("MM-dd-yyy"))

$yesterday = $now.AddDays(-1).ToString(("MM-dd-yyy"))

foreach ($vm in (Get-VM -Name $vms)) {

    $snapshot = $vm | Get-Snapshot -Name "$($vm.Name)-$($yesterday)"

    if ($snapshot) {

        $snapshot | Remove-Snapshot -confirm:$false

    ]

    New-Snapshot -VM $vm -Name "$($vm.Name)-$($today)" -Description "$today"

}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

tdubb123
Expert
Expert
Jump to solution

Thanks Luc!

0 Kudos