首页 > 解决方案 > PowerShell 用户输入、菜单、子菜单、

问题描述

我正在尝试创建一个交互式 PowerShell 脚本,它将执行以下操作:

菜单 1 - 提示用户输入文件路径。然后根据文件路径,我将 cd 进入目录

菜单 2 - 一旦用户输入完成,我将有第二个菜单提示用户选择要解析的文件

一位用户选择将输出文件的选项,然后从菜单 2 重新启动

我不明白如何只显示第一个菜单,然后一旦提交用户输入跳转到第二个菜单,一旦用户选择并解析文件 - 回到第二个菜单直到“Q”。

$Filepath = Read-Host -Prompt 'Please Enter File Path'
do
cd $FilePath

function Show-Menu {
    Clear-Host

    Write-Host "1: Press '1' for parsing test.txt"
    Write-Host "2: Press '2' for parsing test2.txt"
    Write-Host "3: Press '3' for parsing test3.txt"
    Write-Host "Q: Press 'Q' to quit."
}

do {
    Show-Menu $selection = Read-Host "Please make a selection"
    switch ($selection) {
        '1' {
            'You chose option #1'
            Clear-Host
            Import-Csv txt.file -Delimiter '|' -Header '1' ,'2' | Out-GridView
        }
    }
    pause
} until ($selection -eq 'q')

标签: powershellparsingmenu

解决方案


您的帖子中没有两个菜单。你只有一个。除非你说你正在考虑 Read-Host 一个菜单。

这是你想要完成的吗?

Clear-Host

$Filepath = Read-Host -Prompt "`nPlease Enter File Path"
Push-Location -Path $Filepath


$MenuOptions = @'
"Press '1' for parsing test1.txt"
"Press '2' for parsing test2.txt"
"Press '3' for parsing test3.txt"
"Press 'Q' to quit."
'@

"`n$MenuOptions"

while(($selection  = Read-Host -Prompt "`nSelect a option") -ne 'Q')
{
    Clear-Host

    "`n$MenuOptions"

    switch( $selection )
    {
        1 { 'Code for doing option 1 stuff' }
        2 { 'Code for doing option 2 stuff' }
        3 { 'Code for doing option 3 stuff' }
        Q { 'Quit' }
        default {'Invalid entry'}
    }

    Pop-Location
}

推荐阅读