Output from one command -> List of choices for next prompt?

Welcome Forums General PowerShell Q&A Output from one command -> List of choices for next prompt?

Viewing 5 reply threads
  • Author
    Posts
    • #23455
      Participant
      Topics: 5
      Replies: 10
      Points: 16
      Rank: Member

      Hello,
      I’m currently working on a script that would gather output generated from a command and use that dynamic output as a list of choices for the next prompt.
      For example:

      echo “List of New Hire OUs:”
      Get-ADOrganizationalUnit -LDAPFilter ‘(name=*)’ -SearchBase ‘OU=New Hire Staging,OU=service accounts,dc=MyDomain,dc=net’ -SearchScope OneLevel | ft name

      would return this output:
      name
      —-
      3-30-2015
      4-6-2015
      4-13-2015

      I would prefer the output to look like this:
      1) 3-30-2015
      2) 4-6-2015
      3) 4-13-2015

      and then the user could choose 1-3. The list is dynamic and could have any number of options. The associated value of the output (i.e. 3-30-2015, 4-6-2015) would be assgined to a variable called $startdate.

      I’ve got the code all set to execute every other command if the actual full date is entered, but I’d rather give the user a list of choices and have them pick a single number from that.

    • #23456
      Participant
      Topics: 5
      Replies: 40
      Points: 1
      Rank: Member

      Hello Tony,

      You can use the “Out-GridView -PassThru” command for this. It will provide you a list in Gridview and lets you choose one. That one will go in the pipeline for further processing.

      For an example of this take look at http://avbpowershell.nl/use-powershell-to-reset-password-and-unlock-account/

      In short the code in an other example:

    • #23457
      Inactive
      Topics: 9
      Replies: 2322
      Points: 0
      Rank: Member

      If you don’t mind a bit of GUI interaction, this is quite easy thanks to Out-GridView:

      What you’re describing with a text menu is also possible; it just requires a bit more coding on your part. Out-GridView, on the other hand, is a simple one-liner.

    • #23459
      Participant
      Topics: 5
      Replies: 10
      Points: 16
      Rank: Member

      Thanks, I see where you’re going with this. The problem I run in to is that $selectedOUName now has the *entire* output:

      PS C:\scripts> echo $selectedOUName

      <br />Name<br />----<br />3-30-2015<br />

      And my command which includes the $selectedOUName subsequently fails.
      <br />Organizational unit "@{Name=3-30-2015}" was not found. Please make sure you have typed it correctly.<br />

      How can I got this so that $selectedOUName would contain only “3-30-2015”?

    • #23460
      Inactive
      Topics: 9
      Replies: 2322
      Points: 0
      Rank: Member

      Ah, that just takes a little bit of additional code:

    • #23466
      Participant
      Topics: 5
      Replies: 10
      Points: 16
      Rank: Member

      Thank you, that worked!

Viewing 5 reply threads
  • The topic ‘Output from one command -> List of choices for next prompt?’ is closed to new replies.