从阵列选择选项 [英] Select option from Array

查看:266
本文介绍了从阵列选择选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在一个侧项目,并使其更容易为同治,因为几乎所有的从服务器名称是15 charactors长时间,我开始寻找一个RDP同治选项,但没有,我喜欢;于是我开始写一个,我到只有一个问题,我该怎么办来管理,如果用户键入不足以进行搜索,这样两台服务器将匹配查询。我想我将不得不把它放在一个数组,然后让他们选择他们的意思服务器。以下是我迄今为止

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
    }
}
}

基本上,我正在寻找一种方式来管理,如果用户键入的服务器1

这是它会询问他是否要连接到的 Server10或Server11 的,因为他们都匹配过滤。

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

推荐答案

用户的另一个选择为presenting选择是出的GridView ,与 -OutPutMode 开关。

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

这是马特的例如借款:

$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}   

这个例子允许选择多个文件夹,但你可以通过简单地切换他们限制到一个文件夹 -OutPutMode 来单

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

这篇关于从阵列选择选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆