VMware Cloud Community
SENNAF1
Enthusiast
Enthusiast
Jump to solution

Using Get-VIEvent to search for when VMs were PoweredOFF

For an application installation I need to PowerOFF and PowerON (not reboot) all my servers. I created a simple script that pulls my VMs from a csv so I can control when they are powered on and off. 

I would now like a script to show me if they actually powered off then back on and the time they did this.

I figured Get-VIEvent would be good for this. I looked in the web client and see events for the poweroff.

This is great now I can use Get-vievent and parse the information I need.  Wrong!! Smiley Happy

I found this artical from Virtu-Al.net http://www.virtu-al.net/2010/02/01/powercli-working-with-events/

His instructions got me the key and this works.

Get-VM <vmname> | Get-VIEvent | Where { $_.Key -eq 123456}

I modified it to only search one VM to start so I can get the information I need.

Get-VM <vmname>| Get-VIEvent | Where { $_.FullFormattedMessage -eq "is powered off"}

When I run this it returns nothing.

What am I missing.  I would like to browse by cluster and out put a csv with Name,poweredOff time and Powered On time.

Thanks.

Tags (2)
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

You can do something like this

$start = (Get-Date).AddDays(-1)

Get-VIEvent -MaxSamples ([int]::MaxValue) -Start $start |

where{$_ -is [VMware.Vim.VmPoweredOffEvent] -or $_ -is [VMware.Vim.VMPoweredOnEvent]} |

Select CreatedTime,@{N='VM';E={$_.Vm.Name}},@{N='Type';E={$_.GetType().Name}}

If you want to do this per cluster, then the following is most probably the fastest method.

It adds a condition on the Where-clause which checks if the VM displayname is in the list of VMs from the cluster

$clusterName = 'MyCluster'

$clusterVM = Get-Cluster -Name $clusterName | Get-VM | %{$_.Name}

$start = (Get-Date).AddDays(-1)

Get-VIEvent -MaxSamples ([int]::MaxValue) -Start $start |

where{($_ -is [VMware.Vim.VmPoweredOffEvent] -or $_ -is [VMware.Vim.VMPoweredOnEvent]) -and $clusterVM -contains $_.Vm.Name} |

Select CreatedTime,@{N='VM';E={$_.Vm.Name}},@{N='Type';E={$_.GetType().Name}}


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

View solution in original post

2 Replies
LucD
Leadership
Leadership
Jump to solution

You can do something like this

$start = (Get-Date).AddDays(-1)

Get-VIEvent -MaxSamples ([int]::MaxValue) -Start $start |

where{$_ -is [VMware.Vim.VmPoweredOffEvent] -or $_ -is [VMware.Vim.VMPoweredOnEvent]} |

Select CreatedTime,@{N='VM';E={$_.Vm.Name}},@{N='Type';E={$_.GetType().Name}}

If you want to do this per cluster, then the following is most probably the fastest method.

It adds a condition on the Where-clause which checks if the VM displayname is in the list of VMs from the cluster

$clusterName = 'MyCluster'

$clusterVM = Get-Cluster -Name $clusterName | Get-VM | %{$_.Name}

$start = (Get-Date).AddDays(-1)

Get-VIEvent -MaxSamples ([int]::MaxValue) -Start $start |

where{($_ -is [VMware.Vim.VmPoweredOffEvent] -or $_ -is [VMware.Vim.VMPoweredOnEvent]) -and $clusterVM -contains $_.Vm.Name} |

Select CreatedTime,@{N='VM';E={$_.Vm.Name}},@{N='Type';E={$_.GetType().Name}}


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

SENNAF1
Enthusiast
Enthusiast
Jump to solution

I figured out what was wrong with my script.  I forgot to add a wildcard * in the Where-Object statement.  I also changed the operator as well to -like.

In any case your scrip worked perfectly and I stopped making mine. 

Thanks for the help this worked great.

0 Kudos