Read multiple values from console

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

  • Author
    Posts
  • #11769
    Profile photo of Doug Morrison
    Doug Morrison
    Participant

    Hi New to Powershell here, trying to fumble my way through...

    I'm trying to write a script to output the last 3 eventlog items from named servers to an HTML file.

    I want to have a prompt ask the user which servers they want to query. I can't quite work out how to handle multiple inputs, I think I need an array but not sure how to set it up. Can anyone give me any pointers?

    TIA

    Doug

  • #11770
    Profile photo of Don Jones
    Don Jones
    Keymaster

    The BEST way is to let PowerShell do it. At the top of your script:

    [CmdletBinding()]
    Param(
      [Parameter(Mandatory=$True)][string[]]$ComputerName
    )
    

    If someone runs the script, they'll be prompted for the computer name. They can keep entering them, one at a time, hitting Enter on an empty prompt to indicate they're done. The nice thing is that they can bypass the prompt by running the script and providing a -ComputerName parameter up front.

    This remains consistent with the way the shell works for all commands.

  • #11773
    Profile photo of Doug Morrison
    Doug Morrison
    Participant

    Great, thank you Don. I'll play around with that now 🙂

  • #11774
    Profile photo of Doug Morrison
    Doug Morrison
    Participant

    Works a charm, thanks again!

You must be logged in to reply to this topic.