首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >用于软件自动化的Powershell脚本

用于软件自动化的Powershell脚本
EN

Stack Overflow用户
提问于 2021-08-30 09:10:11
回答 1查看 296关注 0票数 0
代码语言:javascript
复制
function show-menu
{

      Clear-Host
      write-host "**********************************************"
      write-host "LIST OF SOFTWARES"

      write-host " 1. googlechrome"
      write-host " 2. firebox"

      write-host " 3. CodeBlocks"
      write-host " 4. windbg"

      
      write-host " 5. nasm"
      write-host " 6. explorer suite"
      

      write-host " 7.pestudio"
      write-host " 8.vscode"

      write-host " 9. sysinternals"
      write-host " 10. python"

      write-host " q. Exit the script"
      write-host " ************************************************"

 }


do
{
   show-menu

      $UserInput = read-host "Enter the software number to be installed "

           switch($UserInput)
           {

                1 {googlechrome;
                   
                    pause
                   }

                2 {firefox;pause}

                3 {codeblocks;pause}
                4 {windbg;pause}

                
                5 {nasm;pause}
                6 {explorersuite;pause}
               

                7 {pestudio;pause}
                8 {vscode;pause}

                9{sysinternals;pause}
                10{python;pause}

                q {break}

                default {write-host "Error in selection, choose 1,2,3,4,5,6,7,8,9,10 or q";pause}
            }
}

while ($UserInput -ne 'q')

$Packages = 'googlechrome',
            'firefox',
            'codeblocks',
            'windbg',
            'nasm',
            'explorersuite',
            'pestudio',
            'vscode',
            'sysinternals',
            'python'

If(Test-Path -Path "$env:ProgramData\Chocolatey")
{

      ### Installing Packagers

     ForEach($PackageName in $Packages)
     {
        choco install $PackageName -y
     }

}


    Else
    {
      

       Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]:: SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))

       ###### package install stuff ###############

       ForEach($PackageName in $Packages)
       {
            Choco install $PackageName -Y
       }

    }

脚本不能正常工作。当我输入任何数字时,软件没有下载,但当我退出时,软件正在下载,如果我的脚本中有任何错误,请告诉我如何做到这一点,在我的系统中已经安装了一些软件,但脚本正在下载该软件,但在安装过程中,脚本给出了软件已经安装的信息。我需要在我的脚本中放一些条件,如果软件已经安装在pc上,脚本不应该下载软件,它应该只是给出像软件已经安装一样的消息。请帮助我完成我的任务,谢谢

感谢你

代码语言:javascript
复制
    enter code here

   do
{
        show-menu

        $Packages = 'googlechrome','firefox','codeblocks', 'windbg','nasm','explorersuite','pestudio','vscode','sysinternals','python'
        $UserInput = read-host "Enter the software number to be installed "

      
         switch($UserInput)
         {

             case1: {googlechrome;pause}

                    If(Test-Path -Path "$env:ProgramData\Chocolatey")
                     {

                                ### Installing Packagers

                            ForEach($PackageName in $Packages)
                            {
                                    choco install $PackageName -y
                             }

                        }


    
                    Else
                    {
                          ############### INSTALLING CHOCOLATEY ####################################################################################
                         ########### Before Executing the script type the command in the powershell terminal to get the admin rights ##############
                         ##########   Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass ##################################################

                        Set-ExexutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]:: SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))

                         ###### package install stuff ###############

                          ForEach($PackageName in $Packages)
                          {
                              choco install $PackageName -Y
                          }

                      }



               case2: {firefox;pause}


                If(Test-Path -Path "$env:ProgramData\Chocolatey")
                {

                        ### Installing Packagers

                     ForEach($PackageName in $Packages)
                     {
                        choco install $PackageName -y
                     }

                 }

Sir is it correct way iam getting missing statement block in switch statement clause
EN

回答 1

Stack Overflow用户

发布于 2021-08-30 13:07:25

好的,我将更改脚本以执行以下操作:

代码语言:javascript
复制
# Step 1) install Chocolatey when needed
if (-not (Test-Path -Path "$env:ProgramData\Chocolatey\choco.exe" -PathType Leaf)) {
   # from https://chocolatey.org/install
   Set-ExecutionPolicy Bypass -Scope Process -Force
   [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072
   Invoke-Expression ((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'

# 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,2} means right align with spaces to max 2 characters
        Write-Host ('{0,2}. {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"
    # 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
    }
    else {
        $availableOptions = 1..$Packages.Count -join ','
        Write-Host "Error in selection, choose $availableOptions or q" -ForegroundColor Red
    }

    $null = Read-Host "Press Enter to continue"
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68981535

复制
相关文章

相似问题

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