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

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

问题描述

我正在做一个副项目,为了让管理更容易,因为几乎所有的服务器名称都是 15 个字符长,我开始寻找 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
    }
}
}

基本上我正在寻找一种方法来管理用户是否键入 server1

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

它会询问他是要连接到 Server10 还是 Server11,因为它们都匹配过滤器.

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

推荐答案

另一个向用户呈现选择的选项是 Out-GridView,带有 -OutPutMode 开关.

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

借鉴马特的例子:

$selection = Get-ChildItem C:	emp -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 切换到 Single

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天全站免登陆