Luke
Luke

Reputation: 667

Select option from Array

I am working on a side project and to make it easier for managment since almost all of out server names are 15 charactors long I started to look for an RDP managment option but none that I liked; so I started to write one and I am down to only one issue, what do I do to manage if the user types not enough for a search so two servers will match the Query. I think I will have to put it in an array and then let them select the server they meant. Here is what I have so far

function Connect-RDP
{

param (
    [Parameter(Mandatory = $true)]
    $ComputerName,
    [System.Management.Automation.Credential()]
    $Credential
)

# take each computername and process it individually
$ComputerName | ForEach-Object{
    Try
    {
        $Computer = $_
        $ConnectionDNS = Get-ADComputer -server "DomainController:1234" -ldapfilter "(name=$computer)" -ErrorAction Stop | Select-Object -ExpandProperty DNSHostName
        $ConnectionSearchDNS = Get-ADComputer -server "DomainController:1234" -ldapfilter "(name=*$computer*)" | Select -Exp DNSHostName            
        Write-host $ConnectionDNS
        Write-host $ConnectionSearchDNS
        if ($ConnectionDNS){
        #mstsc.exe /v ($ConnectionDNS) /f
        }Else{
        #mstsc.exe /v ($ConnectionSearchDNS) /f
        }
    }
    catch
    {
        Write-Host "Could not locate computer '$Computer' in AD." -ForegroundColor Red
    }
}
}

Basically I am looking for a way to manage if a user types server1

that it will ask does he want to connect to Server10 or Server11 since both of them match the filter.

Upvotes: 2

Views: 8716

Answers (2)

Matt
Matt

Reputation: 46710

I'm sure what mjolinor has it great. I just wanted to show another approach using PromptForChoice. In the following example we take the results from Get-ChildItem and if there is more than one we build a collection of choices. The user would select one and then that object would be passed to the next step.

$selection = Get-ChildItem C:\temp -Directory

If($selection.Count -gt 1){
    $title = "Folder Selection"
    $message = "Which folder would you like to use?"

    # Build the choices menu
    $choices = @()
    For($index = 0; $index -lt $selection.Count; $index++){
        $choices += New-Object System.Management.Automation.Host.ChoiceDescription ($selection[$index]).Name, ($selection[$index]).FullName
    }

    $options = [System.Management.Automation.Host.ChoiceDescription[]]$choices
    $result = $host.ui.PromptForChoice($title, $message, $options, 0) 

    $selection = $selection[$result]
}

$selection

-Directory requires PowerShell v3 but you are using 4 so you would be good.

In ISE it would look like this:

ISE Choice

In standard console you would see something like this

Console Choice

As of now you would have to type the whole folder name to select the choice in the prompt. It is hard to get a unique value across multiple choices for the shortcut also called the accelerator key. Think of it as a way to be sure they make the correct choice!

Upvotes: 6

mjolinor
mjolinor

Reputation: 68243

Another option for presenting choices to the user is Out-GridView, with the -OutPutMode switch.

Borrowing from Matt's example:

$selection = Get-ChildItem C:\temp -Directory

If($selection.Count -gt 1){
   $IDX = 0
   $(foreach ($item in $selection){
   $item | select @{l='IDX';e={$IDX}},Name
   $IDX++}) |
   Out-GridView -Title 'Select one or more folders to use' -OutputMode Multiple |
   foreach { $selection[$_.IDX] }
 }

 else {$Selection}   

This example allows for selection of multiple folders, but can you can limit them to a single folder by simply switching -OutPutMode to Single

Upvotes: 7

Related Questions