Butcha
Contributor
Contributor

Using Get-VIEvent To Report VM Creation Info

Good day all!!

I have an interesting issue regarding the use of get-vievent. I have a need to run Get-VIEvent to get a list of VM's that have been created and related info for a specified time frame, lets say the last 30 days. I can get that info just fine using Get-VIEvent! However, this info is returned to me in the following format:

"createdTime : 9/10/2009 10:00:01 AM* *userName :UserID

fullFormattedMessage : Deploying XVMWMB72 on host xxx.yyy.zzz in datacenter from template TemplateX"

This result is great if I was the only one needing this info AND if I just wanted to stop my inquiry here, BUT I would love to utilize this info even more (like taking the VM returned here and doing more inquiries on that object, i.e VM configuration, etc.)

The problem that I'm seeing is that the object returned by Get-VIEvent seems to only have these properties:

#1 createdtime

#2 fullFormattedMessage

#3 Username

What I really need is an additional property that contains the VM name that I can then pull into a sub-script for further processing. Does anyone have any ideas as to how this can be accomplished??

0 Kudos
4 Replies
Zsoldier
Expert
Expert

I would suggest utilizing Al's Daily Report Script. It would get you this information and more in a nicely formatted HTML e-mail.

http://www.virtu-al.net/2009/08/18/powercli-daily-report-v2/

K. Chris Nakagaki (Zsoldier)

http://tech.zsoldier.com

Chris Nakagaki (中垣浩一)
Blog: https://tech.zsoldier.com
Twitter: @zsoldier
LucD
Leadership
Leadership

Alan's script is a great tool and will give you a lot more than the create information.

If you just want the create info have a look at Get VM Create Date & Time


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

Butcha
Contributor
Contributor

LucD,

Thanks for that link! I was able to use it for my needs and it worked great, but as in most of life, there was an issue when I tried to expand the script a little bit.

Below is an excerpt from the script:

Get-VIEvent -Start $start -MaxSamples $eventNr |` Where-Object {$_.GetType().Name -eq "VmCreatedEvent" -or $_.Gettype().Name-eq "VmBeingDeployedEvent"} | % { $row = "" | Select Date, User, VmName, Cluster, Host, Msg $row.Date = $_.createdTime $row.Msg = $_.fullFormattedMessage $row.User = $_.userName $row.VMName = $_.vm.name $t = New-Object VMware.Vim.ManagedObjectReference $t.type = $_.computeResource.computeResource.type $t.Value = $_.computeResource.computeResource.Value $row.Cluster = (Get-View $t).Name $t.type = $_.host.host.type $t.Value = $_.host.host.Value $row.Host = (Get-View $t).Name $report += $row

When I run just this portion, it works great: I get a list of VM's created during the time period specified, along with who created them and when they were created. The reason this isnt sufficient though is because the script results also include VDI virtual desktop creation events, which I do NOT want in the report. Then I noticed that the $row.VMName variable was fed $_.vm.name, which would be the VM name of the current object being processed. So, I figured a filter at the end of the script could kick those virtual desktops out of the report. When I tried to grab that variable and insert it into another line like this:

if ($row.VMName -inotlike "xvm*") { Get-VM -Name $row.VMName | Select-Object "Name",{$_.Guest.OSFullName},{$_.MemoryMB},{$_.HardDisks.Length},{$_.NumCPU} | ft -AutoSize}

the script craps out with errors that dont really give me a sense of what the issue really is.

Here is the error reported:

"+ function PipelineBreakpointerE4078E3092DF4dd9A469F3DC0CBB505C( $offset){BEGIN {} PROCESS {$host.SetShouldExit($offset); $_} <<<< END {}};$vE4078E3092DF4dd9A469F3DC0CBB505C={Remove-PSBreakPoint -BreakPoint $bE4078E3092DF4dd9A469F3DC0CBB505C;Remove-Variable vE4078E3092DF4dd9A469F3DC0CBB505C;Remove-Variable bE4078E3092DF4dd9A469F3DC0CBB505C; The pipeline has been stopped. At :line:28 char:2 + <<<< Get-VM -Name $row.VMName | Select-Object "Name",{$_.Guest.OSFullName},{$_.MemoryMB},{$_.HardDisks.Length},{$_.NumCPU} | ft -AutoSize} Operation is not valid due to the current state of the object. At :line:0 char:0"

0 Kudos
LucD
Leadership
Leadership

I'm not sure why you get that cryptic error message.

It would require me to see the complete script to be able to analyse the error.

If I understood correctly what you want to list, then you could use something like the following one-liner.

Get-VIEvent -Start $start -MaxSamples $eventNr | `
Where-Object {$_.GetType().Name -eq "VmCreatedEvent" -or $_.Gettype().Name-eq "VmBeingDeployedEvent"} | `
Where-Object {$_.vm.name -inotlike "xvm*"} | % {
	Get-VM -Name $row.VMName
} | Select-Object "Name",{$_.Guest.OSFullName},{$_.MemoryMB},{$_.HardDisks.Length},{$_.NumCPU} | ft -AutoSize


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

0 Kudos