【问题标题】:selecting multiple options from the menu list从菜单列表中选择多个选项
【发布时间】:2021-11-10 04:28:46
【问题描述】:
    Set-ExecutionPolicy Bypass -Scope Process -Force;
   [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072;
   iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1')) 
}


# Step 2) define the array of packages you are offering

$Packages = 'googlechrome',
            'firefox',
            'codeblocks',
            'windbg',
            'nasm',
            'explorersuite',
            'pestudio',
            'vscode',
            'sysinternals',
            'python',
            'ccleaner',
            'anaconda3',
            'wireshark',
            'sublimetext3',
            'notepadplusplus',
            'Exit'



# Step 3) define the Show-Menu function

 function Show-Menu
 {
    Clear-Host
    Write-Host "**********************************************"
    Write-Host "LIST OF SOFTWARES"

    # write the options using the array of packages

    for ($i = 0; $i -lt $Packages.Count; $i++) 
    {
        # {0,10} means right align with spaces to max 2 characters
        Write-Host ('{0,10}. {1}' -f ($i + 1), $Packages[$i])
    }

    Write-Host " q. Exit the script"
    Write-Host "*************************************************"
    Write-Host
}

# Step 4) enter an endless loop you only exit if the user enters 'q'

while ($true) 
{
    Show-Menu

    # $UserInput = Read-Host "Enter the software number to be installed"

      $UserInput = Read-Host "Select the softwares number(s) to be installed"
      $ok = $UserInput -match '[123456789101112131415]+$'

      if( -not $ok)
      {
            write-host "Invalid selection"
            sleep 2
            write-host ""
       }
       until ($ok)

       switch -Regex ($UserInput)
       {
         "1" {googlechrome}
         "2" {firefox}
         "3" {codeblocks}
         "4" {windbg}
         "5" {nasm}
         "6" {explorersuite}
         "7" {pestudio}
         "8" {vscode}
         "9" {sysinternals}
         "10" {python}
         "11" {ccleaner}
         "12" {anaconda3}
         "13" {wireshark}
         "14" {sublimetext3}
         "15" {notepadplusplus}
        

        } until ($ok)

        
    # test if the user wants to quit and if so, break the loop
   if ($UserInput -eq 'q') { break }

    # test if the user entered a number between 1 and the total number of packages (inclusive)

    if ([int]::TryParse($UserInput,[ref]$null) -and 1..$Packages.Count -contains [int]$UserInput) 
    {
        # here you install the chosen package using the array index number (= user input number minus 1)
        $packageIndex = [int]$UserInput - 1
        Write-Host "Installing $($Packages[$packageIndex])"
       # Choco install $Packages[$packageIndex] -y
         Choco install $Packages[$packageIndex] -y --ignore-checksums

    }

    else 
    {
        $availableOptions = 1..$Packages.Count -join ','
        Write-Host "Error in selection, choose $availableOptions or q" -Foreground Color Red
    }

     $null = Read-Host "Press Enter to continue"
  
}

我已经编写了脚本,当用户选择数字时,将下载并安装相应的软件。现在不是选择一个选项,而是用户从菜单列表中选择多个选项,然后该软件将被并行下载和安装,所以我修改了我的脚本以选择多个选项,但脚本不起作用,请告诉我如何实现这一点功能。提前致谢

【问题讨论】:

    标签: powershell input options multitasking chocolatey


    【解决方案1】:

    我已经删除了(对我而言)在我的环境中没有意义或产生错误的部分 - 从 $Packages 中删除了“退出”选项并添加了 foreach 部分。所以现在用户可以输入几个空格分隔的数字,以便 $UserInput 包含例如1 2。然后将其拆分为 foreach 部分中的迭代。

    Set-ExecutionPolicy Bypass -Scope Process -Force;
    [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072;
    iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))
    
    # Step 2) define the array of packages you are offering
    
    $Packages = 'googlechrome',
                'firefox',
                'codeblocks',
                'windbg',
                'nasm',
                'explorersuite',
                'pestudio',
                'vscode',
                'sysinternals',
                'python',
                'ccleaner',
                'anaconda3',
                'wireshark',
                'sublimetext3',
                'notepadplusplus'   
    
    
     # Step 3) define the Show-Menu function
    
     function Show-Menu
     {
        Clear-Host
        Write-Host "**********************************************"
        Write-Host "LIST OF SOFTWARES"
    
        # write the options using the array of packages
    
        for ($i = 0; $i -lt $Packages.Count; $i++) 
        {
            # {0,10} means right align with spaces to max 2 characters
            Write-Host ('{0,10}. {1}' -f ($i + 1), $Packages[$i])
        }
    
        Write-Host " q. Exit the script"
        Write-Host "*************************************************"
        Write-Host
    }
    
    # Step 4) enter an endless loop you only exit if the user enters 'q'
    
    while ($true) 
    {
        Show-Menu
    
        $UserInput = Read-Host "Select the softwares number(s) to be installed (space separated)"
    
        # test if the user wants to quit and if so, break the loop
        if ($UserInput -eq 'q') { break }
    
        foreach($input in $UserInput.Split(' ')) {
            # test if the user entered a number between 1 and the total number of packages (inclusive)
    
            if ([int]::TryParse($input,[ref]$null) -and 1..$Packages.Count -contains [int]$input) 
            {
                # here you install the chosen package using the array index number (= user input number minus 1)
                $packageIndex = [int]$input - 1
                Write-Host "Installing $($Packages[$packageIndex])"
                Choco install $Packages[$packageIndex] -y --ignore-checksums
            } else {
                $availableOptions = 1..$Packages.Count -join ','
                Write-Host "Error in selection, choose $availableOptions or q" -Foreground Color Red
            }
        }
    
        $null = Read-Host "Press Enter to continue"
    }
    

    【讨论】:

    • 脚本正在运行,但是当用户输入退出时它没有出现它再次显示选择要安装的软件编号,通常当用户输入 q 时它应该出现在外面,即终端必须显示 PS C:\Windows\system32>
    • 通过将 q 的测试移出 foreach 循环来解决此问题。脚本会相应更新。
    猜你喜欢
    • 1970-01-01
    • 2020-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多