PowerCLI

 View Only
Expand all | Collapse all

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

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

    Posted Mar 31, 2011 04:55 PM

    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



  • 2.  RE: Get-VIEvent - How do I export the event type ? error, warning, or info

    Posted Mar 31, 2011 06:25 PM

    Try it like this (works for me)

    Get-VIEvent | %{
        if($_.GetTYpe().Name -like "*warning*"){
            $type = "Warning"
        }    
    elseif($_.Datacenter){         $type = "Error"
        }     else{         $type = "Info"
        }    
    Add-Member -InputObject $_ -Name EventCategory -Value $type -MemberType NoteProperty -PassThru
    } | Export-Csv "C:\events-category.csv" -NoTypeInformation -UseCulture


  • 3.  RE: Get-VIEvent - How do I export the event type ? error, warning, or info

    Posted Mar 31, 2011 06:59 PM

    Hi,

    Thanks for your help... this doesn't seem to work for me, it doesn't categorize any as ERROR.  for example, when I query for -Types ERROR, I see some "NoAccessUserEvent" events but they get categorizes as INFO.    What is ($_.DC) exactly?

    Thanks!

    Jeff



  • 4.  RE: Get-VIEvent - How do I export the event type ? error, warning, or info

    Posted Mar 31, 2011 07:10 PM

    Sorry, $_.DC should have been $_.Datacenter.

    Try again please.



  • 5.  RE: Get-VIEvent - How do I export the event type ? error, warning, or info

    Posted Mar 31, 2011 07:33 PM

    nope, same problem.  I see a bunch of errors with no DataCenter listed.



  • 6.  RE: Get-VIEvent - How do I export the event type ? error, warning, or info

    Posted Mar 31, 2011 07:40 PM

    Can you give me some of the error types that do not have a datacenter MoRef ?



  • 7.  RE: Get-VIEvent - How do I export the event type ? error, warning, or info

    Posted Mar 31, 2011 07:52 PM

    sure...

    BadUsernameSessionEvent
    ScheduledTaskFailedEvent
    HostCnxFailedBadUsernameEvent
    LicenseServerUnavailableEvent
    NoAccessUserEvent

    I'm thinking there has to be a better way... like knowing exactly how vmware categories them. whether there is some way to surface that atrribute, or knowing the exact rules vmware uses to categorize them.



  • 8.  RE: Get-VIEvent - How do I export the event type ? error, warning, or info

    Posted Mar 31, 2011 08:52 PM

    Strange, tested with BadUsernameSessionEvent and that has a Datacenter value.

    I don't think there is a better way.

    On the EventCollector you can specify the Severity, but the property is not in the regular Event object.

    Only in the EventEx object there is a severity property.

    That's why I came up with that script, but apparently it doesn't work in all cases.

    Btw, I tested with

    Get-VIEvent -Types Error -MaxSamples 9999 | %{
        if($_.GetTYpe().Name -like "*warning*"){
            $type = "Warning"    }
        elseif($_.Datacenter){
            $type = "Error"    }
        else{
            $type = "Info"    }
        Add-Member -InputObject $_ -Name EventCategory -Value $type -MemberType NoteProperty -PassThru
    } |
    Group-Object -Property EventCategory

    In all cases, I only get 1 group with 9999 entries.



  • 9.  RE: Get-VIEvent - How do I export the event type ? error, warning, or info

    Posted May 10, 2011 01:29 PM

    Hi LucD,

         I want to extract Event log for a particular ESX host for a particular time range. like

    Want logs for the server VATLKESX1VMP039 of below dates .Want to see if there were any network issues with this server during the mentioned time.

    03/21/11 00:19:04

    03/20/11 17:23:59

    03/07/11 01:18:43

    Please let me know if its works



  • 10.  RE: Get-VIEvent - How do I export the event type ? error, warning, or info

    Posted May 10, 2011 01:33 PM

    I'm afraid you can't specify multiple time ranges with the Get-VIEvent cmdlet.

    It means you have to make multiple calls to the cmdlet and combine the resturned events.



  • 11.  RE: Get-VIEvent - How do I export the event type ? error, warning, or info

    Posted May 10, 2011 01:38 PM

    Sorry for the confusion  i want to extract for a particular month and for a particular ESX like March ist to 31ist March.
    since i am not good in powerCli script pls let me know the script for extracting the logs

    Want logs for the server VATLKESX1VMP039 of below dates .Want to see if there were any network issues with this server during the mentioned time.



  • 12.  RE: Get-VIEvent - How do I export the event type ? error, warning, or info

    Posted May 10, 2011 01:50 PM

    If you want the month of March, you could do

    Get-VIEvent -Entity VATLKESX1VMP039* -Start (Get-Date 03/01/2011) -Finish (Get-Date 04/01/2011)

    Is that what you are looking for ?



  • 13.  RE: Get-VIEvent - How do I export the event type ? error, warning, or info

    Posted May 10, 2011 02:02 PM

    Yes that i am looking I appreciate your prompt help  but 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.VI
    Object".
    At line:1 char:20
    + Get-VIEvent -Entity <<<<  VATLKESX1VMP039 -Start (Get-Date 03/01/2011) -Finis
    h (Get-Date 04/01/2011)
        + CategoryInfo          : InvalidArgument: (:) [Get-VIEvent], ParameterBin
       dingException
        + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,VMware.VimAutomat
       ion.ViCore.Cmdlets.Commands.GetEvent

    Any thing need to edit ?



  • 14.  RE: Get-VIEvent - How do I export the event type ? error, warning, or info

    Posted May 10, 2011 02:07 PM

    Try using the asterisk (*) behind the name of the server as I showed in my sample line.



  • 15.  RE: Get-VIEvent - How do I export the event type ? error, warning, or info

    Posted May 10, 2011 02:17 PM

    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



  • 16.  RE: Get-VIEvent - How do I export the event type ? error, warning, or info

    Posted May 10, 2011 03:06 PM

    LucD  Need your help on this script.



  • 17.  RE: Get-VIEvent - How do I export the event type ? error, warning, or info

    Posted May 10, 2011 05:07 PM

    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.



  • 18.  RE: Get-VIEvent - How do I export the event type ? error, warning, or info

    Posted May 11, 2011 08:13 AM

    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



  • 19.  RE: Get-VIEvent - How do I export the event type ? error, warning, or info

    Posted May 11, 2011 08:52 AM

    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



  • 20.  RE: Get-VIEvent - How do I export the event type ? error, warning, or info

    Posted May 11, 2011 09:28 AM

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



  • 21.  RE: Get-VIEvent - How do I export the event type ? error, warning, or info

    Posted May 11, 2011 09:36 AM

    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)



  • 22.  RE: Get-VIEvent - How do I export the event type ? error, warning, or info

    Posted May 11, 2011 09:43 AM

    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 ?



  • 23.  RE: Get-VIEvent - How do I export the event type ? error, warning, or info

    Posted May 11, 2011 09:48 AM

    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



  • 24.  RE: Get-VIEvent - How do I export the event type ? error, warning, or info

    Posted May 11, 2011 10:02 AM

    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



  • 25.  RE: Get-VIEvent - How do I export the event type ? error, warning, or info

    Posted May 11, 2011 10:05 AM

    The <CR><LF> was apparently dropped.

    I updated the code section in my previous answer.



  • 26.  RE: Get-VIEvent - How do I export the event type ? error, warning, or info

    Posted May 11, 2011 10:38 AM

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



  • 27.  RE: Get-VIEvent - How do I export the event type ? error, warning, or info

    Posted May 11, 2011 11:05 AM

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



  • 28.  RE: Get-VIEvent - How do I export the event type ? error, warning, or info

    Posted May 11, 2011 11:17 AM

    thanks :smileyhappy:



  • 29.  RE: Get-VIEvent - How do I export the event type ? error, warning, or info

    Posted Feb 21, 2021 05:38 PM

    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!



  • 30.  RE: Get-VIEvent - How do I export the event type ? error, warning, or info

    Posted Feb 21, 2021 06:10 PM

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



  • 31.  RE: Get-VIEvent - How do I export the event type ? error, warning, or info

    Posted Feb 21, 2021 07:14 PM

    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".



  • 32.  RE: Get-VIEvent - How do I export the event type ? error, warning, or info

    Posted Feb 21, 2021 09:18 PM

    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.



  • 33.  RE: Get-VIEvent - How do I export the event type ? error, warning, or info

    Posted Feb 23, 2021 07:14 PM

    Thanks LucD!

    I had a custom permission Vcenter.

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



  • 34.  RE: Get-VIEvent - How do I export the event type ? error, warning, or info
    Best Answer

    Posted Mar 31, 2011 11:16 PM

    Hello, horningj-

    I worked out a couple of items that should grab the event category.  The first selects a few properties, including a calculated property that gets the event category:

    ## works well if no events of type 'EventEx'
    ## get the .Net View object of the EventManager (used for getting the event Category)
    $viewEventMgr = Get-View EventManager
    ## get some VIEvents, select a few properties, including a calculated property for EventCategory
    Get-VIEvent | Select FullFormattedMessage, CreatedTime, @{n="EventCategory"; e={$strThisEventType = $_.GetType().Name; ($viewEventMgr.Description.EventInfo | ?{$_.Key -eq $strThisEventType}).Category}}

    This gets VIEvents (the latest 100, as I did not specify the -MaxSamples parameter), and returns the given properties.  The "EventCategory" calculated property uses the VIEvent object's type to look in the collection of EventDescriptionEventDetail items in the eventInfo property found in the .Net View object for the EventManager.  It then grabs the "Category" property of the corresponding EventDescriptionEventDetail item.

    Works pretty well unless you have any VIEvents of type "EventEx" -- then, this method of "lookup" in the EventDescriptionEventDetail collection fails, as there are more than one item of that type (there are 91 of them at the moment).

    This led me to the next bit.  It is similar to the previous method, but it handles EventEx events, too:

    ## get the .Net View object of the EventManager (used for getting the event Category)
    $viewEventMgr = Get-View EventManager

    ## get some VIEvents (the last 100, as "-MaxSamples" is not specified)
    Get-VIEvent | %{
        ## put the pipeline varible into another variable; get its type
        $oThisEvent = $_; $strThisEventType = $_.GetType().Name
        ## if this event is of type "EventEx"
        if ($strThisEventType -eq "EventEx") {
            $strEventTypeId = $oThisEvent.EventTypeId;
            ## get the EventInfo item (of type EventDescriptionEventDetail) whose "FullFormat" property begins with the EventTypeId of the VIEvent at hand, and get its "Category" property
            $strCategory = ($viewEventMgr.Description.EventInfo | ?{$strRegexPattern = "^$strEventTypeId\|.*"; $_.FullFormat -match $strRegexPattern}).Category
        }
    ## end if
        ## else, can just grab the EventInfo item whose "Key" is the same as this event's type
        else {$strCategory = ($viewEventMgr.Description.EventInfo | ?{$_.Key -eq $strThisEventType}).Category}
        ## add a NoteProperty "EventCategory" to this event
        $oThisEvent | Add-Member -MemberType NoteProperty -Name EventCategory -Value $strCategory -PassThru
    } |
    Select FullFormattedMessage, CreatedTime, EventCategory

    It appears that the EventTypeId of the event returned from Get-VIEvent is included as the first part of the FullFormat property of EventDescriptionEventDetail items with the key EventEx, delimited from the rest of the value by a vertical pipe.  So, the EventTypeId from the VIEvents can be used to do a match on the EventEx types of events from the EventManager .Net View object to get the event "category" (info, warning, error, user).

    You can, of course, change up the Select statements to pick/choose the pieces of info that you would like to export, and then export them to some data file as you please.

    How does that do for you?

    * Message was edited by mattboren on 05 Apr 2011 -- added line to start of second piece of code "$viewEventMgr = Get-View EventManager".  This was already in the first piece and assumed that user was running both pieces in same session, but added for completeness.



  • 35.  RE: Get-VIEvent - How do I export the event type ? error, warning, or info

    Posted Apr 01, 2011 07:11 AM

    Great find, didn't think of that.



  • 36.  RE: Get-VIEvent - How do I export the event type ? error, warning, or info

    Posted Apr 04, 2011 04:55 PM

    well, I tried both methods against a few vSphere servers and EventCategory is always blank.

    wondering if it's a permissions thing or library not installed thing maybe...



  • 37.  RE: Get-VIEvent - How do I export the event type ? error, warning, or info

    Posted Apr 05, 2011 04:39 AM

    Hey, horningj-

    There was a line in the first piece of code that was assumed to have been run in the same session in which the second piece of code was run.  The

    $viewEventMgr = Get-View EventManager

    line.  I have now added that to my previous post for completeness.  If you had started a new session or connected to a new vCenter server, and had run that second section of code without this line, you would surely have gotten all blanks for EventCategory.  So, if you have not, try it again with that line included.

    Another thing -- are you running this when connected to a vCenter server, or are you connected directly to an ESX/ESXi host?  If the latter, you may need to amend this aforementioned line to be as follows:

    $viewEventMgr = Get-View (Get-View 'ServiceInstance').Content.EventManager


    What kind of results does that give you?



  • 38.  RE: Get-VIEvent - How do I export the event type ? error, warning, or info

    Posted Apr 12, 2011 06:50 PM

    Hi,

    I'm connecting to a vCenter server... still not working for me , eventcategory is always blank.  hmmm... I'll see if I can get elevated privs to see if that is the issue.

    Thanks,

    Jeff



  • 39.  RE: Get-VIEvent - How do I export the event type ? error, warning, or info

    Posted Apr 12, 2011 08:29 PM

    that was it!  when I was granted temporary admin access, I was able to see EventCategory... but as a 'Read-Only' user, I was not.  now... I wonder what privs need to be set to be able to see the eventcategories...obviously, read-only doesn't cut it, and I should have to be admin. any ideas?