VMware Cloud Community
Guv
Enthusiast
Enthusiast

Powered off VM's and how long they have been powered off by.

Is it possible to get a script to show the powered off VM's in a cluster ande how long they have been powered off in days.

11 Replies
iw123
Commander
Commander

Hi,

To list powered off VMs you can use the following:

Get-VM | where {$_.PowerState -eq "PoweredOff"}

To list VMs that have been powered off for a certain length of time, the following posts should be of use:

http://communities.vmware.com/message/1674355

http://www.lucd.info/2010/02/09/events-part-5-powered-of-for-more-than-1-week/


Cheers!

*Please, don't forget the awarding points for "helpful" and/or "correct" answers
0 Kudos
LucD
Leadership
Leadership

You can use the following code

$vmOff = Get-Cluster MyCluster | Get-VM | where {$_.PowerState -eq "PoweredOff"}

Get-VIEvent
-Entity $vmOff -MaxSamples ([int]::MaxValue) | where {$_ -is [VMware.Vim.VmPoweredOffEvent]} | Group-Object -Property {$_.Vm.Name} | %{   $lastPO = $_.Group | Sort-Object -Property CreatedTime -Descending | Select -First 1 | Select -ExpandProperty CreatedTime
 
New-Object PSObject -Property @{     VM = $_.Group[0].Vm.Name
   
"Last Poweroff"= $lastPO
   
Duration = [math]::Round((New-TimeSpan -Start $lastPO | Select -ExpandProperty TotalDays))   } }


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

wreedMH
Hot Shot
Hot Shot

LucD,

From the code on your website, is it possible to add a column of the date it was powered off? Also only get VMs powered off LONGER than 30 days. This seems to be picking up VMs powered off today.

1

2

3

4

5

$vms = Get-VM | where {$_.PowerState -eq "PoweredOff"}

$vmPoweredOff = $vms | %{$_.Name}

$events = Get-VIEvent -Start (Get-Date).AddDays(-7) -Entity $vms | where{$_.FullFormattedMessage -like "*is powered off"}

$lastweekVM = $events | %{$_.Vm.Name}

$vmPoweredOff | where {!($lastweekVM -contains $_)}

0 Kudos
LucD
Leadership
Leadership

Try like this

$now = Get-Date

$vmOff = Get-Cluster MyCluster | Get-VM | where {$_.PowerState -eq "PoweredOff"}


Get-VIEvent -Entity $vmOff -MaxSamples ([int]::MaxValue) | where {$_ -is [VMware.Vim.VmPoweredOffEvent]} |

Group-Object -Property {$_.Vm.Name} | %{

  $lastPO = $_.Group | Sort-Object -Property CreatedTime -Descending | Select -First 1 | Select -ExpandProperty CreatedTime

  if((New-TimeSpan -Start $lastPO -End $now).TotalDays -gt 30){

   New-Object PSObject -Property @{

   VM = $_.Group[0].Vm.Name

   "Last Poweroff"= $lastPO

   Duration = [math]::Round((New-TimeSpan -Start $lastPO | Select -ExpandProperty TotalDays))

   }

  }

}


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

0 Kudos
thebart
Enthusiast
Enthusiast

$now = Get-Date
$vmOff = Get-Cluster cluster| Get-VM | where {$_.PowerState -eq "PoweredOff"}


Get-VIEvent -Entity $vmOff -MaxSamples ([int]::MaxValue) | where {$_ -is [VMware.Vim.VmPoweredOffEvent]} |

Group-Object -Property {$_.Vm.Name} | %{

$lastPO = $_.Group | Sort-Object -Property CreatedTime -Descending | Select -First 1 | Select -ExpandProperty CreatedTime}

if((New-TimeSpan -Start $lastPO -End $now).TotalDays -gt 30){

New-Object PSObject -Property @{

VM = $_.Group[0].Vm.Name

"Last Poweroff"= $lastPO

Duration = [math]::Round((New-TimeSpan -Start $lastPO | Select -ExpandProperty TotalDays))
}}

 

ERROR:
New-TimeSpan : Cannot bind parameter 'Start' to the target. Exception setting "Start": "Cannot convert null to type "System.DateTime"."
At line:11 char:27
+ if((New-TimeSpan -Start $lastPO -End $now).TotalDays -gt 30){
+ ~~~~~~~
+ CategoryInfo : WriteError: (:) [New-TimeSpan], ParameterBindingException
+ FullyQualifiedErrorId : ParameterBindingFailed,Microsoft.PowerShell.Commands.NewTimeSpanCommand

0 Kudos
LucD
Leadership
Leadership

Looks like $lastPO is empty.
Can you check if that event has a valid CreatedTime entry?


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

0 Kudos
thebart
Enthusiast
Enthusiast

If I run:

$now = Get-Date
$vmOff = Get-Cluster cluster | Get-VM | where {$_.PowerState -eq "PoweredOff"}


Get-VIEvent -Entity $vmOff -MaxSamples ([int]::MaxValue)

I see output.

Anything after that has no output.

 

0 Kudos
thebart
Enthusiast
Enthusiast

Is VMware.Vim.VmPoweredOffEvent deprecated? 

vmware 6.7u3

powershell

Major Minor Build Revision
----- ----- ----- --------
5 1 14393 5127

0 Kudos
LucD
Leadership
Leadership

No, the VmPoweredOffEvent has not been deprecated.

Not sure what you mean by "Anything after that has no output."
Remember that how far you can go back in time depends on for how long you have set the Task and Event retention dates in your vCenter.


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

0 Kudos
thebart
Enthusiast
Enthusiast

Here are my DB settings:

Max connections: 50
Maximum connections50
Task cleanupEnabled
Task retention (days)60
Event cleanupEnabled
Event retention (days)60

I did notice that I am only logging informational level and not debug, but i'm not sure if that applies to this issue.

When I run "Get-VIEvent -Entity $vmOff -MaxSamples ([int]::MaxValue)" I get tons of output like below:

Info : VMware.Vim.TaskInfo
Key : 114444412
ChainId : 114444412
CreatedTime : 8/8/2022 6:31:49 AM
UserName : xxxx
Datacenter : VMware.Vim.DatacenterEventArgument
ComputeResource : VMware.Vim.ComputeResourceEventArgument
Host : VMware.Vim.HostEventArgument
Vm : VMware.Vim.VmEventArgument
Ds :
Net :
Dvs :
FullFormattedMessage : Task: Remove snapshot
ChangeTag :

 

but when I run "Get-VIEvent -Entity $vmOff -MaxSamples ([int]::MaxValue) | where {$_ -is [VMware.Vim.VmPoweredOffEvent]}"

I get nothing.

 

0 Kudos
LucD
Leadership
Leadership

That Logging settings only defines how much logging goes into the vpxd log file, has nothing to do with events.

You seem to be getting the Task event for the power off, but not the event that the power off has been completed.
Does the power off completes?
In other words do you seen the power off task as being completed?
Also, what Task and Events do you see for that VM in the web client?

It could be interesting to check what kind of events are present for that VM

$start = (Get-Date).AddDays(-1)
Get-VIEvent -Start $start -MaxSamples ([int]::maxvalue) -Entity $vmOff |
Group-Object -Property {$_.Gettype().Name}


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

0 Kudos