首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用powershell构建交互式菜单

使用powershell构建交互式菜单
EN

Stack Overflow用户
提问于 2022-08-03 15:18:32
回答 1查看 275关注 0票数 1

下面的脚本将显示菜单和selection.However提示,如果选择错误,这不会给出重新选择的选项。

我想重新写这样一种方式,它应该给出选项,以确认选择,如果选择是错误的,应该给一个选项重新选择从菜单。可以使用“做直到”循环吗?任何帮助都是非常感谢的

代码语言:javascript
复制
write-host ""
Write-host -ForegroundColor yellow "Choose which Cluster you want to gather ratios on:"
write-host "(it may take a few seconds to build the list)"
write-host ""

$ICLUSTER = get-cluster -server $VIServer | Select-Object Name | Sort-object Name
if ($null -eq $ICLUSTER)
{
    Update-log "Unable to find Cluster Information.Please verify the cluster status before proceed `n"
    break 
}
else
{
$i = 1
$ICLUSTER | %{Write-Host $i":" $_.Name; $i++}
$HCLUSTER = Read-host "Choose the name of cluster you want to select on by entering corresponding number:"
$SCLUSTER = $ICLUSTER[$HCLUSTER -1].Name
Update-log "You have selected $($SCLUSTER). `n"
start-sleep -s 3
}
EN

回答 1

Stack Overflow用户

发布于 2022-08-03 19:21:21

循环&几个额外的if语句应该会让您达到这个目的:

代码语言:javascript
复制
# break the loop by setting $x not equal to 1
$x = 1 

While ($x -eq 1){
$ICLUSTER = get-cluster -server $VIServer | Select-Object Name | Sort-object Name
if ($null -eq $ICLUSTER){
    Update-log "Unable to find Cluster Information. Please verify the cluster status before proceed `n"
    $x = 2
    }
else{
    $i = 1
    $ICLUSTER | %{Write-Host $i":" $_.Name; $i++}
    $HCLUSTER = Read-host "Choose the name of cluster you want to select on by entering corresponding number:"
    $SCLUSTER = $ICLUSTER[$HCLUSTER -1].Name

    if ($SCLUSTER -in $ICLUSTER.Name){
        Write-Host "You have selected $($SCLUSTER), do you want to continue?"
        $yesno = Read-host "Please type 'Y' to continue or 'N' to quit"
        if ($yesno -eq "Y"){
            $x = 1
            }
    else {Write-Host "Quitting"
              $x = 2
              }
    Write-Host "Performing commands"
    # Add commands here, to do the work & complete the task
    $x = 2
    }
elseif (-not($SCLUSTER -in $ICLUSTER.Name)){
    Write-Host "Your selection was not found, Would you like to try again?"
    $yesno = Read-host "Please type 'Y' to try again or 'N' to quit"
    if ($yesno -eq "Y"){
        Write-Host "Trying this again"
        $x = 1
        }
else {Write-Host "Quitting"
      $x = 2
      }
}
start-sleep -s 3
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73223801

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档