在 运行 powershell 脚本中添加菜单选项

Add menu options in a running powershell script

我正在使用带有交互式控制台菜单的 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 客户端。 有时您必须将新服务器添加到服务器列表中, 比如腻子之类的。所以我想在 运行 脚本时向我的菜单添加新选项。 也许有一个函数 "add a new server to list"。 但我不知道如何实现这一点。 我的同事也会使用这个凭证,他们可能有其他服务器,所以菜单必须是某种 它的每个用户都是动态的。 每个人都应该在不接触脚本的情况下建立自己的列表。

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

谢谢!

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

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

希望对您有所帮助