VMware Cloud Community
horningj
Contributor
Contributor
Jump to solution

Get-VIEvent - How do I export the event type ? error, warning, or info

Hi,

I know the Get-VIEvent  cmd lets you specify the type of events to retrieve...   i.e [-Types <EventCategory[]>]

but, I want to extract all events and export them to a CSV...I want one of the columns to be 'EventCategory' which will be either ERROR, WARNING, or INFO but I can't seem to find it.  $_.Gettype().Name gets me the event type but not the category.  I know I can have my script run 3 times (each time specifying the -Types parameter) but I want to run just once.  Any ideas?

Thanks!

Jeff

Reply
0 Kudos
38 Replies
KUMARNILAY
Contributor
Contributor
Jump to solution

I tried both * and as well as FQDN name also but still its shows error

[vSphere PowerCLI] C:\> Get-VIEvent -Entity VATLKESX1VMP039* -Start (Get-Date 03
/01/2011) -Finish (Get-Date 04/01/2011)
Get-VIEvent : Cannot bind parameter 'Entity'. Cannot convert the "VATLKESX1VMP0
39*" value of type "System.String" to type "VMware.VimAutomation.Sdk.Types.V1.V
IObject".
At line:1 char:20
+ Get-VIEvent -Entity <<<<  VATLKESX1VMP039* -Start (Get-Date 03/01/2011) -Fini
sh (Get-Date 04/01/2011)
    + CategoryInfo          : InvalidArgument: (:) [Get-VIEvent], ParameterBin
   dingException
    + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,VMware.VimAutomat
   ion.ViCore.Cmdlets.Commands.GetEvent

****************************************************


[vSphere PowerCLI] C:\> Get-VIEvent -Entity VATLKESX1VMP039.nam.pwcinternal.com*
  -Start (Get-Date 03/01/2011) -Finish (Get-Date 04/01/2011)
Get-VIEvent : Cannot bind parameter 'Entity'. Cannot convert the "VATLKESX1VMP0
39.nam.pwcinternal.com*" value of type "System.String" to type "VMware.VimAutom
ation.Sdk.Types.V1.VIObject".
At line:1 char:20
+ Get-VIEvent -Entity <<<<  VATLKESX1VMP039.nam.pwcinternal.com*  -Start (Get-D
ate 03/01/2011) -Finish (Get-Date 04/01/2011)
    + CategoryInfo          : InvalidArgument: (:) [Get-VIEvent], ParameterBin
   dingException
    + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,VMware.VimAutomat
   ion.ViCore.Cmdlets.Commands.GetEvent

Reply
0 Kudos
KUMARNILAY
Contributor
Contributor
Jump to solution

LucD  Need your help on this script.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Are the following returning anything ?

Get-VMHost VATLKESX1VMP039

or

Get-VMHost VATLKESX1VMP039*

I have the impression there might be a problem with the name you pass.


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

Reply
0 Kudos
KUMARNILAY
Contributor
Contributor
Jump to solution

LucD,

  2nd one throws out put. Let me know what next need to do

[vSphere PowerCLI] C:\> Get-VMHost VATLKESX1VMP039
Get-VMHost : 5/11/2011 4:10:58 AM    Get-VMHost        VMHost with name 'VATLKE
SX1VMP039' was not found, using the specified filter(s).
At line:1 char:11
+ Get-VMHost <<<<  VATLKESX1VMP039
    + CategoryInfo          : ObjectNotFound: (:) [Get-VMHost], VimException
    + FullyQualifiedErrorId : Core_OutputHelper_WriteNotFoundError,VMware.VimA
   utomation.ViCore.Cmdlets.Commands.GetVMHost

*************************************************************

[vSphere PowerCLI] C:\> Get-VMHost VATLKESX1VMP039*

Name            ConnectionState PowerState      Id CpuUsage CpuTotal  Memory  M
                                                        Mhz      Mhz UsageMB em
                                                                             or
                                                                             yT
                                                                             ot
                                                                             al
                                                                             MB
----            --------------- ----------      -- -------- -------- ------- --
vatlkesx1vmp... Connected       PoweredOn  ...6949    27726    57600   94524 69

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Ok, let's try to get the events now.

Can you do

$esx = Get-VMHost VATLKESX1VMP039*

Get-VIEvent -Entity $esx -Start (Get-Date 03/01/2011) -Finish (Get-Date 04/01/2011)

This should retrieve the events for the month of March


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

Reply
0 Kudos
KUMARNILAY
Contributor
Contributor
Jump to solution

Thanks!! it worked and i am greatful to you.Can we filter like error and warning. I have attached the output

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Sure, use something like this. It will only show events of the type Error or Warning.

$esx = Get-VMHost VATLKESX1VMP039*

Get-VIEvent -Entity $esx -Types "Error","Warning" -Start (Get-Date 03/01/2011) -Finish (Get-Date 04/01/2011)


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

Reply
0 Kudos
KUMARNILAY
Contributor
Contributor
Jump to solution

Worked Perfect  so if i want to  export the evetslogs to excel format then

$esx = Get-VMHost VATLKESX1VMP039*

Get-VIEvent -Entity $esx -Types "Error","Warning" -Start (Get-Date 03/01/2011) -Finish (Get-Date 04/01/2011) > C:\Eventlog.csv

Correct ?

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

No, not really.

You use the Export-Csv cmdlet for that and you use the pipe ('|') to pass the objects from one cmdlet (Get-VIEvent) to the next cmdlet (Export-Csv)

Like this

$esx = Get-VMHost VATLKESX1VMP039*

Get-VIEvent -Entity $esx -Types "Error","Warning" -Start (Get-Date 03/01/2011) -Finish (Get-Date 04/01/2011) | Export-Csv "C:\Eventlog.csv" -NoTypeInformation -UseCulture

Note that Get-VIEvent and Export-Csv are on 1 line.

You can eventually split the line with a back-tick to improve readability. Like this

$esx = Get-VMHost VATLKESX1VMP039*

Get-VIEvent -Entity $esx -Types "Error","Warning" -Start (Get-Date 03/01/2011) -Finish (Get-Date 04/01/2011) | `

Export-Csv "C:\Eventlog.csv" -NoTypeInformation -UseCulture


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

Reply
0 Kudos
KUMARNILAY
Contributor
Contributor
Jump to solution

Gr8

first one worked but 2nd one gives error like
[vSphere PowerCLI] C:\tmp> .\Event.ps1
Get-VMHost : A parameter cannot be found that matches parameter name 'Entity'.
At C:\tmp\Event.ps1:1 char:54
+ $esx = Get-VMHost VATLKESX1VMP039*Get-VIEvent -Entity <<<<  $esx -Types "Erro
r","Warning" -Start (Get-Date 03/01/2011) -Finish (Get-Date 04/01/2011) | `Expo
rt-Csv "C:\Eventlog.csv" -NoTypeInformation -UseCulture
    + CategoryInfo          : InvalidArgument: (:) [Get-VMHost], ParameterBind
   ingException
    + FullyQualifiedErrorId : NamedParameterNotFound,VMware.VimAutomation.ViCo
   re.Cmdlets.Commands.GetVMHost

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

The <CR><LF> was apparently dropped.

I updated the code section in my previous answer.


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

Reply
0 Kudos
KUMARNILAY
Contributor
Contributor
Jump to solution

Thanks LucD its worked out with out any error.One last thing can we specify at what time error or warning came ?

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

The time should be in the CreatedTime column in the CSV file.


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

Reply
0 Kudos
KUMARNILAY
Contributor
Contributor
Jump to solution

thanks Smiley Happy

Reply
0 Kudos
Macleud
Enthusiast
Enthusiast
Jump to solution

Hi .

This works great for Vcenter.

But doesn't work for:

Vm

$entity = Get-VM -Name 'VmName'

Get-VIEvent -Entity $entity

and 

Esxi

$entity =  Get-VMHost -Name 'HostName'

Get-VIEvent -Entity $entity

Any ideas?

Thanks!

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Works for me.
What exactly is not working for you?


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

Reply
0 Kudos
Macleud
Enthusiast
Enthusiast
Jump to solution

I want one of the columns to be 'EventCategory' which will be either ERROR, WARNING, or INFO.

$fileName = '/Users/mac/Downloads/report.csv'
$entity = Get-vm -Name 'test_vm'

Get-VIEvent -Entity $entity -MaxSamples 100 | %{
if($_.GetTYpe().Name -like "*warning*"){
$type = "Warning"
}
elseif($_.VirtualMachine){
$type = "Error"
}
else{
$type = "Info"
}
Add-Member -InputObject $_ -Name EventCategory -Value $type -MemberType NoteProperty -PassThru
} | Export-Csv -Path $fileName -NoTypeInformation -UseCulture

 

As a result the columns "EventCategory" only "Info".

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

I'm not sure where you got that code from, but that is not the correct way to get the event category.
Matt's reply at the beginning of this thread shows how it is done.


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

Reply
0 Kudos
Macleud
Enthusiast
Enthusiast
Jump to solution

Thanks LucD!

I had a custom permission Vcenter.

When I was granted full admin access, I was able to see event category.

Reply
0 Kudos