Question for Don Jones about PowerShell book

This topic contains 2 replies, has 2 voices, and was last updated by Profile photo of digitalbathau digitalbathau 3 years, 11 months ago.

  • Author
    Posts
  • #8858
    Profile photo of digitalbathau
    digitalbathau
    Participant

    Hi mate,

    Easier to do it here.

    This is the part that doesn't make sense to me. In section 3.5.2 it says:

    What if you try to run a command and forget one of the mandatory parameters? Take a look at the help for Get-EventLog, for example, and you’ll see that the -LogName parameter is mandatory—the parameter isn’t enclosed in square brackets. Try running Get-EventLog without specifying a log name.

    But -LogFile IS enclosed in brackets. I know further down the page it goes on explaining positional parameters but as I'm reading the book in sequence and making sure I understand every paragraph until I move onto the next one I struggled with that statement.

    Does that make sense?

    Great book by the way so far 😀

  • #8870
    Profile photo of Don Jones
    Don Jones
    Keymaster

    (So, please use the PowerShell Q&A forum – gets more attention; this forum is intended for site feedback)

    All parameters take the form:

    -ParameterName

    Except switch parameters, which don't have a value type of course.

    [-ParameterName]

    is a mandatory positional parameter. That's what -LogName is on Get-EventLog. Mandatory, because the ENTIRE PARAMETER (including its value) isn't in square brackets. Positional, because the PARAMETER NAME ONLY is in square brackets.

    The book is accurate. While -LogName's PARAMETER NAME is indeed in square brackets, that does not constitute the ENTIRE PARAMETER. Maybe that's the part that's confusing? A parameter is not just its name; it's also the corresponding value. Unless it's a simple switch. Just one of those things you have to get used to.

    By contrast:

    [-ParameterName ]

    is an optional parameter. The entire thing is in [brackets].

    [[-ParameterName] ]

    is an optional, positional parameter. That is, you don't HAVE to specify the parameter at all. However, if you do, you can omit the parameter name provided you put the corresponding value in the correct position.

    If it helps, use the full help (Help Get-EventLog -Full), since the individual parameter documentation makes the whole positional/mandatory thing much clearer.

  • #8873
    Profile photo of digitalbathau
    digitalbathau
    Participant

    Thanks for replying!

    Makes sense.

    You are correct in the part that was confusing me. I was defining (in my mind) that a parameter was JUST the name. Not the name + the value.

You must be logged in to reply to this topic.