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?
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
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
Thanks Luc!