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?
You could also try like this.
Place both conditions in the same While-block
#Subtracts a day from todays date for comparison
$date = Get-Date
$3dayago = $date.adddays(-3)
#gets the exceptions into an array
$exceptions = Get-Content exceptions.txt
Get-VM | Get-Snapshot | where{$_.Created -lt $3dayago -and $exceptions -notcontains $_.VM.Name} |
Remove-Snapshot -Confirm:$false -RunAsync
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
You could also try like this.
Place both conditions in the same While-block
#Subtracts a day from todays date for comparison
$date = Get-Date
$3dayago = $date.adddays(-3)
#gets the exceptions into an array
$exceptions = Get-Content exceptions.txt
Get-VM | Get-Snapshot | where{$_.Created -lt $3dayago -and $exceptions -notcontains $_.VM.Name} |
Remove-Snapshot -Confirm:$false -RunAsync
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thank you, that worked like a charm, I think I was over thinking it.