Powershell 从数组中选择选项
我正在做一个辅助项目,为了便于管理,因为几乎所有的服务器名称都有15个字符长,我开始寻找RDP管理选项,但没有一个是我喜欢的;所以我开始写一篇文章,我只讨论了一个问题,如果用户类型不足以进行搜索,那么我该怎么做才能让两台服务器匹配查询。我想我必须把它放在一个数组中,然后让他们选择他们想要的服务器。这是我到目前为止所拥有的Powershell 从数组中选择选项,powershell,active-directory,powershell-4.0,Powershell,Active Directory,Powershell 4.0,我正在做一个辅助项目,为了便于管理,因为几乎所有的服务器名称都有15个字符长,我开始寻找RDP管理选项,但没有一个是我喜欢的;所以我开始写一篇文章,我只讨论了一个问题,如果用户类型不足以进行搜索,那么我该怎么做才能让两台服务器匹配查询。我想我必须把它放在一个数组中,然后让他们选择他们想要的服务器。这是我到目前为止所拥有的 function Connect-RDP { param ( [Parameter(Mandatory = $true)] $ComputerName,
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
它会问他想连接到服务器10还是服务器11,因为这两个服务器都匹配过滤器。我确定这有什么好处。我只是想展示另一种使用的方法。在下面的示例中,我们从Get ChildItem获取结果,如果有多个,我们将构建一个选项集合。用户将选择一个对象,然后将该对象传递到下一步
$selection=获取子项C:\temp-目录
如果$selection.Count-gt 1{
$title=文件夹选择
$message=您要使用哪个文件夹?
构建选项菜单
$choices=@
对于$index=0;$index-lt$selection.Count;$index++{
$choices+=新对象System.Management.Automation.Host.ChoiceDescription$selection[$index]。名称,$selection[$index]。全名
}
$options=[System.Management.Automation.Host.ChoiceDescription[]]$choices
$result=$host.ui.PromptForChoice$title、$message、$options,0
$selection=$selection[$result]
}
$selection
-目录需要PowerShell v3,但您使用的是4,所以您会很好
在ISE中,它将如下所示:
在标准控制台中,您将看到如下内容
到目前为止,您必须键入整个文件夹名称才能在提示中选择选项。对于快捷键(也称为快捷键),很难在多个选项中获得唯一值。把它看作是确保他们做出正确选择的一种方式 我肯定它有什么好处。我只是想展示另一种使用的方法。在下面的示例中,我们从Get ChildItem获取结果,如果有多个,我们将构建一个选项集合。用户将选择一个对象,然后将该对象传递到下一步
$selection=获取子项C:\temp-目录
如果$selection.Count-gt 1{
$title=文件夹选择
$message=您要使用哪个文件夹?
构建选项菜单
$choices=@
对于$index=0;$index-lt$selection.Count;$index++{
$choices+=新对象System.Management.Automation.Host.ChoiceDescription$selection[$index]。名称,$selection[$index]。全名
}
$options=[System.Management.Automation.Host.ChoiceDescription[]]$choices
$result=$host.ui.PromptForChoice$title、$message、$options,0
$selection=$selection[$result]
}
$selection
-目录需要PowerShell v3,但您使用的是4,所以您会很好
在ISE中,它将如下所示:
在标准控制台中,您将看到如下内容
到目前为止,您必须键入整个文件夹名称才能在提示中选择选项。对于快捷键(也称为快捷键),很难在多个选项中获得唯一值。把它看作是确保他们做出正确选择的一种方式 另一个向用户显示选项的选项是Out-GridView,带有-OutPutMode开关 借用Matt的例子:
$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切换为single将它们限制为单个文件夹。另一个向用户显示选项的选项是Out GridView,带有-OutPutMode开关 借用Matt的例子:
$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切换到single来将其限制为单个文件夹吗?您可能会从模糊名称解析中获益:Get ADComputer-LdapFilter ANR=$Computer | Select-ExpDNSHostName@mjolinor我喜欢它的外观,既然是你写的,你会如何使用它?不管怎样,我弄明白了,它就像一个符咒。有什么帮助吗?您可能会从模糊名称解析中获益:Get ADComputer-LdapFilter ANR=$Computer | Select-ExpDNSHostName@mjolinor我喜欢它的外观,既然是你写的,你会如何使用它?不管怎样,我弄明白了,它就像一个符咒。我喜欢它的外观,所以我会把get-adobject的输出通过管道传输到这个吗?@Luke,是的。但是,您必须对按钮/选项进行其他细微更改,以显示相应的属性。这是ChoiceDescriptionNote之后的第一个参数,但是,如果您有多个选项可供选择,那么这种方法将变得混乱。我喜欢它的外观,所以我将通过管道将get-adobject的输出传递给此?@Luke Yes。豪
无论如何,您必须对按钮/选项进行其他细微更改,以显示相应的属性。这是ChoiceDescriptionNote之后的第一个参数,但是,如果您有多个选项可供选择,那么这种方法将变得混乱。啊,是的。永远忘了我们的GridView。用户友好得多。哦哦哦,我从来没有在Gridview上使用过-outputmode,太棒了。谢谢啊,对。永远忘了我们的GridView。用户友好得多。哦哦哦,我从来没有在Gridview上使用过-outputmode,太棒了。谢谢