Automation

 View Only
  • 1.  Snapshot more than three days but less than 10 days

    Posted Mar 09, 2023 07:10 AM

    How can i get snapshot age more than 3 days but less than 10 days



  • 2.  RE: Snapshot more than three days but less than 10 days
    Best Answer

    Posted Mar 09, 2023 08:49 AM

    Something like this?

    $now = Get-Date
    
    Get-VM |
    Get-Snapshot |
    Where {$_.Created -lt $now.AddDays(-3) -and $_.Created -gt $now.AddDays(-10)}


  • 3.  RE: Snapshot more than three days but less than 10 days

    Posted Mar 09, 2023 09:46 AM

    Thanks man it works fine. but i need to get snapshot more than three days but less than 10 days along with summation of all VM size.

     



  • 4.  RE: Snapshot more than three days but less than 10 days

    Posted Mar 09, 2023 09:51 AM

    The objects returned by Get-Snapshot contain a property SizeGB, is that what you want?



  • 5.  RE: Snapshot more than three days but less than 10 days

    Posted Mar 09, 2023 09:57 AM

    nope i want to get vm who have snapshot more than three days but less than 10 days in a table including the snapshot size in GB and all vm snapshot size sum in the same table is it possible?



  • 6.  RE: Snapshot more than three days but less than 10 days
    Best Answer

    Posted Mar 09, 2023 10:05 AM

    You mean like this?

     

    $now = Get-Date
    
    $snap = Get-VM | Get-Snapshot
    $totalSnapSizeGB = $snap | Measure-Object -Property SizeGB -Sum | Select-Object -ExpandProperty Sum
    
    $snap | 
    Where {$_.Created -lt $now.AddDays(-3) -and $_.Created -gt $now.AddDays(-10)} |
    Select @{N='VM';E={$_.VM.Name}},
        Name, Created, SizeGB,
        @{N='AllSnapShotSizeGB';E={$totalSnapSizeGB}}

     



  • 7.  RE: Snapshot more than three days but less than 10 days

    Posted Mar 09, 2023 10:37 AM

    Awesome this does work for allsnapshotsize but VM name shows blank 

    output

    mahady_hassan_0-1678358224014.png

     



  • 8.  RE: Snapshot more than three days but less than 10 days

    Posted Mar 09, 2023 10:39 AM

    My bad, I forgot to fill in the Expression for the VM calculated property.
    Corrected above



  • 9.  RE: Snapshot more than three days but less than 10 days

    Posted Mar 09, 2023 11:34 AM

    hey luc this script working just fine but it gives a large number of decimal value like 10 to 12 digit for 

    SizeGB and AllsnapshotsizeGB. how to get it like 3 decimal value. below screenshot for your reference

     

    mahady_hassan_0-1678361614343.png

     

     



  • 10.  RE: Snapshot more than three days but less than 10 days
    Best Answer

    Posted Mar 09, 2023 11:43 AM

    You can use the [Math]::Round() method

    $now = Get-Date
    
    $snap = Get-VM | Get-Snapshot
    $totalSnapSizeGB = $snap | Measure-Object -Property SizeGB -Sum | Select-Object -ExpandProperty Sum
    
    $snap | 
    Where {$_.Created -lt $now.AddDays(-3) -and $_.Created -gt $now.AddDays(-10)} |
    Select @{N='VM';E={$_.VM.Name}},
        Name, Created, 
        @{N='SizeGB';E={[math]::Round($_.SizeGB,3)}},
        @{N='AllSnapShotSizeGB';E={[math]::Round($totalSnapSizeGB,3)}}

     



  • 11.  RE: Snapshot more than three days but less than 10 days

    Posted Mar 09, 2023 11:52 AM

    Thanks a lot man. this is working fine.