VMware Cloud Community
valot
Enthusiast
Enthusiast
Jump to solution

How to know since a virtual machine is power off ?

Hi all,

I  want to know if it's possible to find on the database or somewhere else  a paramater which would allow to know since when a Virtual Machine is  power off.

In fact, I want to scan which Virtual Machine are not used and since when...

My Level = vSphere4.1

Tks

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

That is strange, I always find VmPoweredOffEvent entries, even when the user initiated a Shutdown from with in the guest OS.

You can easily test this.

  • Do a Shutdown from within a guest
  • When the VM is powered off, do

Get-VIEvent -Start (Get-Date).AddMinutes(-30) -Entity MyVM | Select CreatedTime,@{N="Event";E={$_.GetType().Name}}

You should see the shutdown you initiated


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

View solution in original post

0 Kudos
13 Replies
Troy_Clavell
Immortal
Immortal
Jump to solution

this looks like a job best suited for VMware vSphere™ PowerCLI  I have moved your discussion.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

You can use the Tasks & Events log.

With the Get-VIEvent cmdlet you can fetch the events.

You would want the VmPoweredOff event.

Have a look at my Events – Part 5 : Powered off for more than 1 week ? post.

In the sample scripts I look for text in the event message, which is another way of doing this.


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

valot
Enthusiast
Enthusiast
Jump to solution

Tks, good idea.

0 Kudos
valot
Enthusiast
Enthusiast
Jump to solution

Hi LuCD,

Tks for you answer.

The probleme to check and track the VMPowerOFF event is if the user shutdown its virtual machines via the Operating System itself this event does't exist.

I already try this anyway.

I also thought to check the last modified date of all .vmdk, it works, but it's sometimes hard to match to .vmdk name with the virtual machine name as some virtual machine names was changed after the creation.

Perhaps a combinated scritp can help me ? 1/ list all .vmdk modified since 1 year (example) 2/ match the virtual machine name  after ?

tks

0 Kudos
LucD
Leadership
Leadership
Jump to solution

That is strange, I always find VmPoweredOffEvent entries, even when the user initiated a Shutdown from with in the guest OS.

You can easily test this.

  • Do a Shutdown from within a guest
  • When the VM is powered off, do

Get-VIEvent -Start (Get-Date).AddMinutes(-30) -Entity MyVM | Select CreatedTime,@{N="Event";E={$_.GetType().Name}}

You should see the shutdown you initiated


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

0 Kudos
valot
Enthusiast
Enthusiast
Jump to solution

You're right LuCD,

I had a look only on the events given by the virtual center, but with your cmd I saw the "power off" event ==>

FullFormattedMessage : MyVM on  MyESX in MyDatacneter is powered off

But the -Entity parameters does't work.

I will try your previous recommandation via yoru web site, the script seemes to be OK for my case.

I will let you know my result.

Tks.

0 Kudos
valot
Enthusiast
Enthusiast
Jump to solution

Hi again 🙂

The script give me only all virtual machine power off, not the virtual machine power off since 1 year in this example.

Sorry I'm not very fluent in PowerCLI scripting.

$vms = (Get-VM | where {$_.PowerState -eq "PoweredOff"}) | %{$_.Name}
$events = Get-VIEvent -Start (Get-Date).AddDays(-365) | where{$_.FullFormattedMessage -like "*is powered off"}
$lastyearVM = $events | %{$_.Vm.Name}
$Since1Year = $vms | where {!($lastyearVM -contains $_)}
$Since1Year > C:\report\VM-Power-OFF-since-1-year.txt

Could you help me ?

tks.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Two remarks;

  • The Get-VIEvent will only return 100 events unless you specify the MaxSamples parameter with a bigger number. I normally do

Get-VIEvent -Start (Get-Date).AddDays(-180) -MaxSamples [int]::MaxValue 

This will theoretically return the maximum integer value events

  • The retention for Tasks and Events is by default set to 180 days. You can change this through the vSphere Client with <Administration><vCenter Server Settings><Database Retention Policy>. So unless this value is set to 365 days in your environment, you will not be able to retrieve events that far back I'm afraid.


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

0 Kudos
valot
Enthusiast
Enthusiast
Jump to solution

1/ I'm testing with the -MaxSamples (500000) parameters to be sure have all events.

I will let you know my result

question : the parameter (Get-Date).AddDays(-365) means I will find all virtual machines power off since more than 1 year in this case ?

2/ I'm not sure the default is 180 days. I checked the database and the table "VPX-EVENTS" return me all events since the beginnig of my infrastructure.

It seems that the default value is "no database retention policy" for vSphere4 and not possible to set it on VI3.

vSphere4 ==>

                         1857545.png

Tks

Best regards;

0 Kudos
LucD
Leadership
Leadership
Jump to solution

1) Yes

I prefer to use [int]::MaxValue instead of a random number on the MaxSamples parameter, that's the maximum value you can store in an [int]

2) Ok, so you should be able to find all events from the last year.


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

0 Kudos
valot
Enthusiast
Enthusiast
Jump to solution

Hi LuCD

About the paramaters - MaxSamples[int]::MaxValue syntax ==> does it mean in mycase -MaxSample 500000 instead of -Maxsamples (500000)

0 Kudos
LucD
Leadership
Leadership
Jump to solution

You should be able to give -MaxSamples 500000


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

valot
Enthusiast
Enthusiast
Jump to solution

I think The script is good for me now.

Tks a lot for your help.

Best Regards.

0 Kudos