powershell - 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')
解决方案
您的帖子中没有两个菜单。你只有一个。除非你说你正在考虑 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
}
推荐阅读
- docker - 为 golang 应用程序构建 docker 容器而不调用 go build
- mysql - 查询以根据结果从一个表中选择,然后从另一个表中选择
- html - div标签宽度太长无法减小
- email - 文本后面是否可以有两个背景图像?
- reactjs - React 路由器仅在刷新后工作
- java - 赋予令牌高于另一个令牌的优先级
- r - 包装 ggplot2 文本以适应绘图宽度
- ios - 基于 NSFetchedResultsControllerDelegate 刷新 tableview
- ios - 表格视图中静态表格单元格的单元格内容对齐
- sql-server - 在 Xamarin Android 上使用 Web API 仅显示部分数据时如何修复错误