VMware Cloud Community
Wayne_He
Contributor
Contributor

How can I confirm if a VM is powered on/off in the past month?

I want to create a script to count the configured  Memory/CPU for some VMs during the past month, just for all powered on VM, ignore the powered off VM in that period. I know how to get the configured CPU and Memory via PowerCLI get-vm, but I don't know if this VM is powered on/off in the past month? or if it was just powered off some days, and other days is powered on.

Who can help me?

4 Replies
cjscol
Expert
Expert

Checking the PowerState property of the VM will only tell you if the VM is currently Powered on of off.

You are probably best to look through the events for the VM to see if that has been powered on or off in the last month, e.g. the following command will get all events for the VM object $vm

Get-VIEvent -Entity $vm

You are looking for "Task: Power On virtual machine" and "Task: Power Off virtual machine", therefore you could use

Get-VIEvent -Entity $vm | Where {$_.FullFormattedMessage -Like "Task: Power O*"


Next you will want to limit it to the month you are interested in, e.g.


Get-VIEvent -Entity $vm -Start 12/01/2015 -Finish 12/31/2015 | Where {$_.FullFormattedMessage -Like "Task: Power O*"}


The start and end dates need to be in the format mm/dd/yyyy or dd/mm/yyyy depending on your regional settings.


You can then Measure this Object to see if there were any events in the month e.g.


If ((Get-VIEvent -Entity $vm -Start 12/01/2015 -Finish 12/31/2015 | Where {$_.FullFormattedMessage -Like "Task: Power O*"}).Count -gt 0)

{

what every script you want to run if the server was powered off or on in the last month

}


This will catch any VM that has been powered on in the month and any VM that was powered off (e.g. a VM that was powered on before the start of the month). What it will not catch is any VM that was powered on before the start of the month and still powered on at the end of the month, without being powered off during the month). So you could get the details of any VM that is currently powered on. You will need to consider the time the script is run and may have to check any VMs that have been powered off after the end of the month and before the script is run


Hope that helps.

Calvin Scoltock VCP 2.5, 3.5, 4, 5 & 6 VCAP5-DCD VCAP5-DCA http://pelicanohintsandtips.wordpress.com/blog LinkedIn: https://www.linkedin.com/in/cscoltock
Wayne_He
Contributor
Contributor

Thanks linotelera, but it's for current powerstat, not past month.

0 Kudos
Wayne_He
Contributor
Contributor

Thanks Cjscol, it's helpful. but if this VM was powered on/off many times in this past month, how can I count all the Powered on period. On other words, how long this VM was Powered on?

0 Kudos