我从我修改的一些代码中创建了一个控制台菜单。
我使用以下代码并使用switch
命令转到此代码中列出的菜单选项:
# new menu for remote tasks ps1
$Title = "Remote Tasks"
$Message = "Please enter your selection below:"
$remoteRestartComputer = New-Object System.Management.Automation.Host.ChoiceDescription "&1 Restart one or more remote computer(s)", `
"Restart one or more remote computer(s)"
$remoteShutdownComputer = New-Object System.Management.Automation.Host.ChoiceDescription "&2 Shutdown one or more remote computer(s)", `
"Shutdown one or more remote computer(s)"
$wakeOnLanComputer = New-Object System.Management.Automation.Host.ChoiceDescription "&3 Start/Wake VM Station", `
"Start/Wake VM Station"
$getOutThisB = new-object System.Management.Automation.Host.ChoiceDescription "&4 Exit", `
"Exit"
$Options = [System.Management.Automation.Host.ChoiceDescription[]]($remoteRestartComputer, $remoteShutdownComputer, $wakeOnLanComputer, $getOutThisB)
$selectRemoteTask = $host.ui.PromptForChoice($Title, $Message, $Options, 0)
问题出在控制台窗口中,它在同一行显示了几个选项,即:
Remote Tasks
Please enter your selection below:
[1] 1 Restart one or more remote computer(s)
[2] 2 Shutdown one or more remote computer(s)[3] 3 Start/Wake VM Station
[4] 4 Exit[?] Help (default is "1"):
有什么方法可以添加换行符,这样看起来更好吗?提前谢谢。
答案 0 :(得分:2)
我刚刚提出这个问题作为这里的一些问题的答案,我甚至不记得它是什么。我认为它与ASCII管道字符有关,以制作边框。这将使您的菜单具有可选标题,并显示它。然后你只需要显示你的Get-Host,把它放在一个循环中,然后转到你的开关。
Function MenuMaker{
param(
[string]$Title = $null,
[parameter(Mandatory=$true,
ValueFromPipeline = $true)][String[]]$Selections
)
$Width = if($Title){$Length = $Title.Length;$Length2 = $Selections|%{$_.length}|Sort -Descending|Select -First 1;$Length2,$Length|Sort -Descending|Select -First 1}else{$Selections|%{$_.length}|Sort -Descending|Select -First 1}
$Buffer = if(($Width*1.5) -gt 78){(78-$width)/2}else{$width/4}
if($Buffer -gt 4){$Buffer = 4}
$MaxWidth = $Buffer*2+$Width+$($Selections.count).length
$Menu = @()
$Menu += "╔"+"═"*$maxwidth+"╗"
if($Title){
$Menu += "║"+" "*[Math]::Floor(($maxwidth-$title.Length)/2)+$Title+" "*[Math]::Ceiling(($maxwidth-$title.Length)/2)+"║"
$Menu += "╟"+"─"*$maxwidth+"╢"
}
For($i=1;$i -le $Selections.count;$i++){
$Item = "$i`. "
$Menu += "║"+" "*$Buffer+$Item+$Selections[$i-1]+" "*($MaxWidth-$Buffer-$Item.Length-$Selections[$i-1].Length)+"║"
}
$Menu += "╚"+"═"*$maxwidth+"╝"
$menu
}
您可以这样称呼它:
MenuMaker -Title $Title -Selections "Restart one or more remote computer(s)","Shutdown one or more remote computer(s)","Start/Wake VM Station","Exit"
当然,它不包括帮助,但它确实为你做了一个漂亮的漂亮菜单。您可以修改该函数以添加-Help开关以显示? - Help
行,或者仅包含帮助作为您的选择之一。然后你就做了类似的事情:
Do{
MenuMaker -Title $Title -Selections "Restart one or more remote computer(s)","Shutdown one or more remote computer(s)","Start/Wake VM Station","Exit"
$Selection = Read-Host "Please enter your selection (? for Help)"}While($Selection -notin (1..4) -or $Selection -eq "?")
修改:切换示例(更新上面的代码以包含?获取帮助!):
Switch($Selection){
4 {Continue}
3 {Run code to Start/Wake VM Station}
2 {Run code to shutdown remote computer}
1 {Run code to restart remote computer}
"?" {Run code to display Help}
}
答案 1 :(得分:2)
以下换行符适用于我
[Environment]::NewLine
答案 2 :(得分:0)
我知道这是一篇非常古老的文章,但我发现您的菜单布局及其做得确实不错。 我对其进行了更新,因为它为我抛出了一些有关特殊字符的错误,因此它可以通过引用Unicode字符来运行。
万一您或其他任何人仍然想使用它,在这里是这样,因此没有其他人不必去查找字符:D
Function New-Menu{
param(
[string]$Title = $null,
[parameter(Mandatory=$true,
ValueFromPipeline = $true)][String[]]$Selections
)
$Width = if($Title){$Length = $Title.Length;$Length2 = $Selections|ForEach-Object{$_.length}|Sort-Object -Descending|
Select-Object -First 1;$Length2,$Length|Sort-Object -Descending|Select-Object -First 1}else{$Selections|ForEach-Object{$_.length}|
Sort-Object -Descending|Select-Object -First 1}
$Buffer = if(($Width*1.5) -gt 78){(78-$width)/2}else{$width/4}
if($Buffer -gt 4){$Buffer = 4}
$MaxWidth = $Buffer*2+$Width+$($Selections.count).length
$Menu = @()
$Menu += "$([char]0x2554)"+"$([Char]0x2550)"*$maxwidth+"$([char]0x2557)"
if($Title){
$Menu += "$([Char]0x2551)"+" "*[Math]::Floor(($maxwidth-$title.Length)/2)+$Title+" "*[Math]::Ceiling(($maxwidth-$title.Length)/2)+"$([Char]0x2551)"
$Menu += "$([Char]0x255F)" +"$([char]0x2500)"*$maxwidth+"$([Char]0x2562)"
}
For($i=1;$i -le $Selections.count;$i++){
$Item = "$i`. "
$Menu += "$([Char]0x2551)"+" "*$Buffer+$Item+$Selections[$i-1]+" "*($MaxWidth-$Buffer-$Item.Length-$Selections[$i-1].Length)+"$([Char]0x2551)"
}
$Menu += "$([char]0x255a)"+"$([Char]0x2550)"*$maxwidth+"$([char]0x255D)"
$menu
}
答案 3 :(得分:0)
部分可能使用转义的退格 (`b)
$OfficeOptions = @($i=0;@("No office","Office 2010","Office 2013","Office 2016") | ForEach-Object {
New-Object System.Management.Automation.Host.ChoiceDescription ("&$i`b`b$($_)" + [Environment]::NewLine + "`b`b`b`b")
$i++
})
$OfficeVersion = $Host.UI.PromptForChoice("Office version", "Choose office version", $OfficeOptions, 0)
然而,它会在每行显示 2 个空格。