Currently trying to make a script to delete all snapshots over 3 days old while referencing a txt file of VMs that are exceptions. What I have is hackish and poorly written, it should work I think, but its not following the list.
#Subtracts a day from todays date for comparison
$date = get-date
$3dayago = $date.adddays(-3)
#gets a list of VM's
$snaps = get-vm | Get-Snapshot
#gets the exceptions into an array
[array]$exceptions = Get-Content exceptions.txt
[string]$exception_list = $null
$exception_list = $exceptions -join "|"
foreach($snap in $snaps | ) {
if($snap.VM -nomatch $exception_list){
remove-snapshot $snap -confirm:$false -RunAsync}
}
Any thoughts, am I going about this the wrong way?