fborges555
Enthusiast
Enthusiast

List snapshots older than and only powered on VM

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

Reply
0 Kudos
LucD
Leadership
Leadership

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


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
fborges555
Enthusiast
Enthusiast

L.

 

for some reason it bring the off and on VM

snapshots.png

 Thanks

Reply
0 Kudos
LucD
Leadership
Leadership

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}}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
rictusempra
Contributor
Contributor

Hi LucD,

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

Reply
0 Kudos
LucD
Leadership
Leadership

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}}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos