我试图删除超过特定日期的快照,如果他们的描述标有PERM
,我不想删除它们。
如果说明标有DELT
,请在特定日期删除它们。
如果VM只有一个快照,但是有多个快照无法检查第二个快照并将其删除,那么一切正常。
$AllSnaps = Get-VM | Get-Snapshot | select VM, Name, Description, sizemb, Created
ForEach ($Snap in $AllSnaps) {
$Identity = $null
$DelDate = $null
Try {
$Identity = $Snap.Description.Substring(0, 4)
$DelDate = $Snap.Description.Substring(5)
}
Catch {
$Identity = $null
$DelDate = $null
}
If ($Identity -eq "DELT" -and $DelDate -ge (Get-Date).ToString("dd-MM-yyyy")) {
write-host "$($snap.VM) - $($snap.Name) - $($snap.Created) -Delete Snaps (Exception Completed)" -fore "blue"
}
ElseIf ($Identity -eq "PERM") {
Write-host -"VMName : $($snap.VM) - SnapName : $($snap.Name) - Created $($snap.Created) - Size $($Snap.sizemb) - Action : Skipping - Reason :Permanent Snapshot"
}
ElseIf ($Snap.created -le ($OldDate).ToShortDateString()) {
write-host "$($snap.VM) - $($snap.Name) - $($snap.Created) - Deleting Snaps Older than 15 Days" -fore "red"
}
}