Get-EventLog and variables

This topic contains 2 replies, has 2 voices, and was last updated by  SuperSysAdmin 2 years, 10 months ago.

  • Author
  • #28494


    I have a strange issue here:

    This works:

    $entrytype = "Error"
    Get-EventLog -LogName System -EntryType $entrytype}

    This also works:

    Get-EventLog -LogName System  -EntryType "Error,Information,Warning,SuccessAudit,FailureAudit"}

    However, this doesn't work:

    $entrytype = "Error,Information,Warning,SuccessAudit,FailureAudit"
    Get-EventLog -LogName System -EntryType $entrytype} 

    The error returned is:

    Cannot validate argument on parameter 'EntryType'. The argument "Error,Information,Warning,SuccessAudit,FailureAudit" does not belong to the set 
    "Error,Information,FailureAudit,SuccessAudit,Warning" specified by the ValidateSet attribute. Supply an argument that is in the set and then try the command again.
        + CategoryInfo          : InvalidData: (:) [Get-EventLog], ParameterBindingValidationException
        + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.GetEventLogCommand

    If you look at the error, I am providing exactly what it wants but still it doesn't like it for some reason, and I cannot figure out why.

    So in short, if I put more than 1 EntryType in a variable, it's not accepted anymore.

    Why o why?

  • #28495

    Don Jones

    Your second example is using an array of strings:


    Your not-working is using a single string:


    The quotes prevent PowerShell from recognizing it as an array. Instead:


    Notice that the commas aren't in quotes.

  • #28498


    Thanks, I was already thinking in that direction, but did not get the syntax right it seems.

You must be logged in to reply to this topic.