【问题标题】:Add menu options in a running powershell script在正在运行的 powershell 脚本中添加菜单选项
【发布时间】:2019-11-14 11:08:20
【问题描述】:

我正在使用带有交互式控制台菜单的 PowerShell 脚本,该菜单使用开关部分中的功能。看起来像这样:

function startmenu {
    do {
        [int]$selection = 0
        while ($selection-lt 1 -or $selection-gt 5){
            Write-Host "1. Search"
            write-host "2. Change"
            Write-Host "3. do stuff"
            write-host "4. Clear Screen"
            write-host "5. Exit"

            [int]$selection= read-host "choose one option"
            switch ($selection) {
                1{find}
                2{change}
                3{dostuff}
                4{cls}
                5{exit}
            }
        }
    }
    while ($selection -ne 4)}

现在我正在尝试使用类似的 ssh 连接菜单构建一个新脚本。 由于 PowerShell 有自己的 ssh 客户端。 有时您必须将新服务器添加到服务器列表中, 像腻子什么的。所以我想在运行脚本时向我的菜单添加新选项。 也许有一个功能“添加一个新的服务器到列表”。 但我不知道如何实现这一点。 我的同事也会使用这个脚本,他们可能有其他服务器,所以菜单必须是某种 动态为它的每个用户。 每个人都应该在不接触脚本的情况下建立自己的列表。

有什么办法吗?我的 PowerShell 知识非常基础。

谢谢!

【问题讨论】:

    标签: powershell ssh scripting menu powershell-ise


    【解决方案1】:

    如果有一个控制台菜单,用户可以在其中添加选项,也许以下内容会让您了解如何做到这一点:

    function Show-Menu {
        [CmdletBinding()]
        param (
            [string[]]$options = @('FileZilla', 'Posh-SSH PowerShell', 'WinSCP','PuTTY')
        )
        # store the options in a List object for easy addition
        $list = [System.Collections.Generic.List[string]]::new()
        $list.AddRange($options)
    
        # now start an endless loop for the menu handling
        while ($true) { 
            Clear-Host
            # loop through the options list and build the menu
            Write-Host "`r`nPlease choose from the list below.`r`n"
            $index = 1
            $list.Sort()
            $list | ForEach-Object { Write-Host ("{0}.`t{1}" -f $index++, $_ )}
            Write-Host "`r`nN.`tAdd a new item to the list"
            Write-Host "Q.`tQuit"
    
            $selection = Read-Host "`r`nEnter Option"
    
            switch ($selection) {
                {$_ -like 'N*' } {
                    # the user want to add a new item to the menu
                    $item = (Read-Host "Please add a new item").Trim()
                    if (![string]::IsNullOrWhiteSpace($item) -and $list -notcontains $item) {
                        Write-Host "Adding new item '$item'.." -ForegroundColor Yellow
                        $list.Add($item)
                    }
                }
                {$_ -like 'Q*' } { 
                    # if the user presses 'Q', exit the function
                    return 
                } 
                default {
                    # test if a valid numeric input in range has been given
                    if ([int]::TryParse($selection, [ref]$index)) {
                        if ($index -gt 0 -and $index -le $list.Count) {
                            # do whatever you need to perform
                            $selection = $list[$index - 1]  # this gives you the text of the selected item
    
                            # for demo, just output on screen what option was selected
                            Write-Host "Building connection using $selection" -ForegroundColor Green
                            # return the selection made to the calling script
                            return $selection
                        }
                        else {
                            Write-Host "Please enter a valid option from the menu" -ForegroundColor Red
                        }
                    }
                    else {
                        Write-Host "Please enter a valid option from the menu" -ForegroundColor Red
                    }
                }
            }
    
            # add a little pause and start over again
            Start-Sleep -Seconds 1
        }
    }
    
    # call the function
    $choice = Show-Menu
    

    希望对你有帮助

    【讨论】:

    • 谢谢!我可以用这个!但是有没有办法配置我在列表中添加的每个条目?我只想将此脚本用于具有不同名称、IP 地址和用户名的 ssh 会话,这些会话需要在每个条目中进行不同的配置。我希望我的解释是可以理解的..所以如果我添加一个新条目,例如。 netapp 和 192.168.12.18 和一个用户名,每次我再次打开这个脚本时都应该保存它。或者也许我必须编写一个像 txt 文件这样的配置文件,我保存更改并在每次打开脚本时加载它,以便连接到我的 ssh 主机。
    • @Yannick 这真的是一个全新的问题。如果你想要所有这些,我强烈建议你为此制作一个 GUI,在字典中添加新条目并将其写入一个配置文件,该文件在窗口打开时读取。请注意您对您还想存储的用户名的密码所做的操作,因为几乎任何人都可以读取配置文件.. 同时.. 作为 SO 新手,您可能不知道这一点,但习惯于 accept the answer that solved your problem by点击左侧的✓。
    • 感谢您的信息。我会查查的。我接受了答案!
    猜你喜欢
    • 2020-09-03
    • 2014-02-15
    • 1970-01-01
    • 1970-01-01
    • 2020-08-12
    • 2021-06-30
    • 2019-08-30
    • 2013-11-14
    • 2022-10-18
    相关资源
    最近更新 更多