Author Posts

March 18, 2015 at 7:19 am

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.

March 18, 2015 at 7:24 am

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:

 $user = Get-ADUser -Filter *  -Properties LockedOut -SearchBase (
            $OUsearchbase | Out-GridView -PassThru -Title "Select the location of the user"
            ) | 
        Select-Object name,samaccountname,LockedOut,enabled | 
        Out-GridView -PassThru -Title "Select the user"

March 18, 2015 at 7:25 am

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

$selectedOUName = Get-ADOrganizationalUnit -LDAPFilter '(name=*)' -SearchBase 'OU=New Hire Staging,OU=service accounts,dc=MyDomain,dc=net' -SearchScope OneLevel |
Select-Object -Property Name |
Out-GridView -Title 'Select an OU' -PassThru

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.

March 18, 2015 at 7:48 am

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


Name
----
3-30-2015

And my command which includes the $selectedOUName subsequently fails.

Organizational unit "@{Name=3-30-2015}" was not found. Please make sure you have typed it correctly.

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

March 18, 2015 at 7:50 am

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

$selectedOUName = Get-ADOrganizationalUnit -LDAPFilter '(name=*)' -SearchBase 'OU=New Hire Staging,OU=service accounts,dc=MyDomain,dc=net' -SearchScope OneLevel |
Select-Object -Property Name |
Out-GridView -Title 'Select an OU' -PassThru |
Select-Object -ExpandProperty Name

March 18, 2015 at 8:25 am

Thank you, that worked!