PowerCLI

 View Only
  • 1.  List snapshots older than and only powered on VM

    Posted Jul 27, 2021 01:11 PM

    Hi gurus 

    I want a list of all powered ON vm with snapshot older than 5 days, I have the following but as you can tell still curving some learning

    $PowONvm = Get-VM |where {$_.powerstate -eq "PoweredOn"}| Get-Snapshot | Where {$_.Created -lt (Get-Date).AddDays(-5)} |select vm,name,powerstate

    This bring powered off and on vm , can someone help on where I went wrong.

     

    Thanks



  • 2.  RE: List snapshots older than and only powered on VM

    Posted Jul 27, 2021 01:22 PM

    The object returned by Get-Snapshot contains a property named VM, under which you find the same object that is returned by Get-VM.
    So you could do

    Get-VM | Get-Snapshot |
        where {$_.VM.powerstate -eq "PoweredOn" -and $_.Created -lt (Get-Date).AddDays(-5)} |
        select vm,name,powerstate


  • 3.  RE: List snapshots older than and only powered on VM

    Posted Jul 27, 2021 01:36 PM

    L.

     

    for some reason it bring the off and on VM

    snapshots.png

     Thanks



  • 4.  RE: List snapshots older than and only powered on VM

    Posted Jul 27, 2021 01:51 PM

    That PowerState is the state the VM was in when the snapshot was taken.
    When you want to see the current powerstate you can do

    Get-VM | Get-Snapshot |
        where {$_.VM.powerstate -eq "PoweredOn" -and $_.Created -lt (Get-Date).AddDays(-5)} |
        select @{N='VM';E={$_.VM.Name}},Name,@{N='PowerState';E={$_.VM.PowerState}}


  • 5.  RE: List snapshots older than and only powered on VM

    Posted Jul 25, 2022 11:11 AM

    Hi LucD,

    can you help me out on how to use this query for the current powerstate in this script?



  • 6.  RE: List snapshots older than and only powered on VM

    Posted Jul 25, 2022 11:36 AM

    You could do something like this

    Get-VM | Get-Snapshot |
    select @{N='VM';E={$_.VM.Name}},
       @{N='Current PowerState';E={$_.VM.PowerState}},
       Name,
       @{N='PowerState';E={$_.VM.PowerState}}