VMware Cloud Community
eabennet
Contributor
Contributor
Jump to solution

Trouble filter out VMs to delete snapshots from.

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?

Tags (1)
Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

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

View solution in original post

2 Replies
LucD
Leadership
Leadership
Jump to solution

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

eabennet
Contributor
Contributor
Jump to solution

Thank you, that worked like a charm, I think I was over thinking it.

Reply
0 Kudos