Automation

 View Only
  • 1.  logical help for get snapshots

    Posted Jan 25, 2022 08:45 AM

    Hi,

    I have a problem on this small report. Somewhere must be something wrong, because the output is not true if id do some tests!

    The Report should only contains VMs´where snaphot is older than 24 hours or vm´s with snapshots older than 5 days(with exception tag).

    The exclusion to hold snaphots until 5 days is a certian tag.

    Based on this report the snapshots will be removed.

    For quick testing the time value is changed to minutes!

    $excludeVM = Get-Content 'C:\00\SnapshotReport\exclude.txt' 
    $includeVM = Get-Cluster test* | Get-VM | where{$excludeVM -notcontains $_.Name} | Sort-Object -Property Name
    $includeVM
    #
    $tag = Get-Tag -Name 'snapshot5days' -Category 'snapshot'
    
    
    clear-variable Snapreport
    $Snapreport = $includeVM | Get-Snapshot | Where {(($_.Created -lt (Get-Date).addminutes(-1)) -and 
    ($_.vm.Name | where{ (Get-TagAssignment -Entity $_).Tag.Id -notcontains $tag.Id})) -or ($_.Created -lt (Get-Date).addminutes(-5))}
    
    $Snapreport

     



  • 2.  RE: logical help for get snapshots

    Posted Jan 25, 2022 09:09 AM

    Can you try with

    Get-Snapshot -VM $includeVM | 
    Where {$_.Created -lt (Get-Date).AddDays(-1) -or 
        ((Get-TagAssignment -Entity $_.VM).Tag.Id -contains $tag.Id -and $_.Created -lt (Get-Date).AddDays(-5))}


  • 3.  RE: logical help for get snapshots

    Posted Jan 25, 2022 10:15 AM

    Hi,

     

    in your proposal  the secound filter after -or has no affect or?

    Is there a way to use the tag name instead the id?

     

     



  • 4.  RE: logical help for get snapshots
    Best Answer

    Posted Jan 25, 2022 10:41 AM

    Correct, you need to add the Tag test as well.

     

    $includeVM | Get-Snapshot | 
    Where {(((Get-TagAssignment -Entity $_.VM).Tag.Name -notcontains $tag.Name -and $_.Created -lt (Get-Date).AddDays(-1)) -or 
        ((Get-TagAssignment -Entity $_.VM).Tag.Name -contains $tag.Name  -and $_.Created -lt (Get-Date).AddDays(-5)))}


    Update: corrected typo in snippet

     



  • 5.  RE: logical help for get snapshots

    Posted Jan 25, 2022 11:11 AM

    thanks Luc,

    one id was to much..:-)  ((Get-TagAssignment -Entity $_.VM).Tag.Id.Name

    $includeVM | Get-Snapshot | 
    Where {(((Get-TagAssignment -Entity $_.VM).Tag.Name -notcontains $tag.Name -and $_.Created -lt (Get-Date).AddDays(-1)) -or 
        ((Get-TagAssignment -Entity $_.VM).Tag.Name -contains $tag.Name  -and $_.Created -lt (Get-Date).AddDays(-5)))}