用于软件自动化的PowerShell脚本 [英] Powershell script for software automation

查看:0
本文介绍了用于软件自动化的PowerShell脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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:ProgramDataChocolatey")
{

      ### 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上,脚本不应该下载软件,它应该只给出像软件已经安装一样的消息。 请帮我完成我的任务 感谢您

感谢您


    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:ProgramDataChocolatey")
                     {

                                ### 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:ProgramDataChocolatey")
                {

                        ### Installing Packagers

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

                 }

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

推荐答案

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

# Step 1) install Chocolatey when needed
if (-not (Test-Path -Path "$env:ProgramDataChocolateychoco.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"
}

这篇关于用于软件自动化的PowerShell脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆