Read-Host with limited values

This topic contains 5 replies, has 3 voices, and was last updated by  Jeremy Corbello 4 months ago.

  • Author
    Posts
  • #89723

    Nemanja Jovic
    Participant

    Hello everyone, basically I have a script that is working in my AD environment, I search for computers/server based on specific criteria and put them into a variable, as a result, I want to be able to present to user values from variable, user should be able to pick one of the values and script will after operate on that value, let's bring one example.

     $Serverlist = (Get-ADComputer -Filter {OperatingSystem -like "*Server*" -and Enabled -eq $True}).Name 

    I want to be able to present values that are stored in $Serverlist variable to the user with Read-Host cmdlet, for example;

     $Answer = Read-Host "Pick one of the servers" 

    But I do not know how to present possible values stored in the variable to user. Any help will be appreciated. Thanks!

  • #89726

    Olaf Soyk
    Participant

    You could output your server list including an index and only ask for the index number to be provided by the user. 😉

    • #89732

      Nemanja Jovic
      Participant

      Hi Olaf, thanks for quick answer, can you please guide me with one example?

  • #89738

    Olaf Soyk
    Participant

    OK, because it's Christmas time ... 😉

    $ServerList = Get-ADComputer -Filter {OperatingSystem -like "*Server*" -and Enabled -eq $True} 
    $ServerList | Select-Object -Property @{Name='Index';Expression={$ServerList.Indexof($_)}},Name
    • #89741

      Nemanja Jovic
      Participant

      Thanks dude, much appreciated, happy Christmas to you and your family 🙂 cheers!

  • #89746

    Jeremy Corbello
    Participant

    A much easier way – use Out-GridView with the -PassThru switch:

    $Serverlist = (Get-ADComputer -Filter {OperatingSystem -like "*Server*" -and Enabled -eq $True}).Name | Out-GridView -PassThru
    

    You'll get a pretty popup with all of the entries. You can select whichever you want (including holding Ctrl to select multiple), then click OK on the bottom right. The entries that you selected will be saved to $Serverlist.

You must be logged in to reply to this topic.