Automation

 View Only
  • 1.  Trouble filter out VMs to delete snapshots from.

    Posted Mar 29, 2017 04:48 PM

    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?



  • 2.  RE: Trouble filter out VMs to delete snapshots from.
    Best Answer

    Posted Mar 29, 2017 04:57 PM

    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



  • 3.  RE: Trouble filter out VMs to delete snapshots from.

    Posted Mar 29, 2017 05:06 PM

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