我正在尝试创建一个交互式PowerShell脚本,该脚本将执行以下操作:
菜单1-提示用户输入文件路径。然后基于文件路径,我将进入目录
菜单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')
答案 0 :(得分:0)
您的帖子中没有两个菜单。你只有一个。除非您说您正在考虑“读取主机”菜单。
这是您要完成的事情吗?
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
}
答案 1 :(得分:0)
不使用循环,另一种选择是调用“菜单”函数本身以在完成解析后再次显示菜单。
您还可以使用Host.ChoiceDescription
来构建并显示菜单。
$Filepath = Read-Host -Prompt 'Please Enter File Path'
Set-Location $FilePath
function Get-Selection {
$Title = "Please make a selection"
$Message = "Select File for parsing"
$Option1 = New-Object System.Management.Automation.Host.ChoiceDescription "test.txt"
$Option2 = New-Object System.Management.Automation.Host.ChoiceDescription "test2.txt"
$Option3 = New-Object System.Management.Automation.Host.ChoiceDescription "test3.txt"
$Option4 = New-Object System.Management.Automation.Host.ChoiceDescription "Quit"
$Options = [System.Management.Automation.Host.ChoiceDescription[]]($Option1,$Option2,$Option3,$Option4)
$host.ui.PromptForChoice($title, $message, $options, 0)
}
function Show-Menu {
switch (Get-Selection) {
0 {
Write-Host 'You chose option #1'
Clear-Host
Import-Csv txt.file -Delimiter '|' -Header '1' ,'2' | Out-GridView
Show-Menu # Show menu for next choice
}
1 {
Write-Host 'You chose option #2'
Clear-Host
# Import-Csv
Show-Menu # Show menu for next choice
}
2 {
Write-Host 'You chose option #2'
Clear-Host
# Import-Csv
Show-Menu # Show menu for next choice
}
3 {
Write-Host 'Quit'
}
}
}
Show-Menu # Load menu when script is run