2

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.

4
  • 1
    Any help? gallery.technet.microsoft.com/scriptcenter/… Commented Jul 17, 2015 at 15:44
  • You might benefit from Ambiguous Name Resolution here: Get-ADComputer -LdapFilter "(ANR=$Computer)" | Select -Exp DNSHostName Commented Jul 17, 2015 at 15:49
  • @mjolinor I like the way this looks, and since you were the one that wrote it how would you reccomend to use it? Commented Jul 17, 2015 at 16:37
  • Nevermind I got it figured out and it works like a charm. Commented Jul 17, 2015 at 16:49

2 Answers 2

7

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

Sign up to request clarification or add additional context in comments.

2 Comments

Ah yes. Always forget about Out-GridView. Much more user friendly.
Oooo I've never used -outputmode on Gridview, thats excellent. Thanks!
6

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!

3 Comments

I like the way this looks, so I would just pipe the output of get-adobject to this?
@Luke Yes. However you would have to make other minor changes to that an appropriate property appears for the buttons/choices. It's the first parameter after ChoiceDescription
Note, however, that this approach will get messy if you have more than a few options to choose from.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.