在正在运行的powershell脚本中添加菜单选项
我使用的是带有交互式控制台菜单的PowerShell脚本,该菜单使用交换机部件中的函数。看起来像这样:在正在运行的powershell脚本中添加菜单选项,powershell,ssh,scripting,menu,powershell-ise,Powershell,Ssh,Scripting,Menu,Powershell Ise,我使用的是带有交互式控制台菜单的PowerShell脚本,该菜单使用交换机部件中的函数。看起来像这样: function startmenu { do { [int]$selection = 0 while ($selection-lt 1 -or $selection-gt 5){ Write-Host "1. Search" write-host "2. Change" Write
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知识非常基础
谢谢 要想拥有一个用户可以添加选项的控制台菜单,以下内容可能会让您了解如何做到这一点:
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 with 192.168.12.18和一个用户名,那么每次我再次打开此脚本时都应该保存该条目。或者我必须编写一个类似txt文件的配置文件,保存更改并在每次打开脚本时加载,以便连接到我的ssh主机。@Yannick这真是一个全新的问题。如果您想要所有这些,我强烈建议您为此制作一个GUI,在字典中添加新条目,并将其写入一个配置文件,该文件在窗口打开时读取。不过,请注意,您也要存储用户名的密码,因为几乎任何人都可以读取配置文件。。同时作为新手,您可能不知道这一点,但习惯于单击✓ 在左边。谢谢你的信息。我会查的。我接受了答案!