【问题标题】:Powershell script for software automation用于软件自动化的 Powershell 脚本
【发布时间】:2021-10-29 01:17:16
【问题描述】:
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 中,则脚本不应该下载软件,它只应该给出软件已经安装的消息。 请帮助我完成我的任务 谢谢你

谢谢你


    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
 

【问题讨论】:

  • 这个问题一直被问到.. 你可能和 [jhansi jatavath](jhansi jatavath) 是同一个人还是在同一个教室?正如我在之前的一个问题中所评论的那样,您的代码中的逻辑在哪里?您显示一个菜单,使用所选选项执行 nothing,然后安装某个变量 $Packages.. 中的所有内容
  • 先生,实际上我们正在合作。可能她会问先生,我们是 power-shell 脚本的新手,我们不知道如何在该脚本中添加逻辑,所以请帮助先生,这对我们会有帮助。
  • 真的没那么难,再想一想:你现在在循环中运行Show-Menu 函数,但只有当有人进入q 时才退出。对于所有其他选项,什么都没有完成。只有当用户使用q(用于QUIT)退出该循环时,安装才会发生无论用户想要安装什么。我的提示:在循环中,您可以在其中测试用户输入的内容,然后在此处安装所选的软件包。删除while ($UserInput -ne 'q')之后的所有内容

标签: powershell automation menu display user-interaction


【解决方案1】:

好的,我会更改脚本来执行此操作:

# 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"
}

【讨论】:

  • 非常感谢先生,先生,当我运行脚本时,软件安装进入程序数据而不是程序文件,如果软件已经安装,那么软件也在下载。
  • @ShravanMeghavath 可能,但这与 PowerShell 无关。您可以使用许多选项并安装切换到 Chocolatey 安装软件的方式和位置。我认为最好的办法是创建一个Packages.config file XML 文件,这样 choco 就知道要为每个提到的软件做什么。您需要研究 choko 在这方面的工作原理。他们甚至提供视频here
  • 先生,作为我的任务的一部分,我必须完成这个脚本,截止日期是 9 月 2 日,请帮帮我先生,1.当我运行 exec 文件时的脚本没有显示在控制面板和桌面图标中创造。 2. 如果软件已经安装,那么软件也在下载 3. 如果对任何软件有任何升级,则必须进行升级。请帮帮我先生。
  • @ShravanMeghavath 抱歉,但我不使用 Chocolately 并查看 commandline switches,我没有看到任何可以首先检查是否安装了软件包的内容。取决于您的版本, 也许this answer 可以帮助你..
  • @Theo 我相信优雅地“导入”已经安装并由 Chocolatey 管理的软件是 Chocolatey Pro 的一个功能。因为我自己不使用 Chocolatey Pro,所以我无法说出它是如何工作的。在我自己的软件包中,我检查是否存在 MSI ProductCode(用于 msi 安装程序),如果作为“穷人的 Chocolatey Pro”安装,则跳过运行安装程序,但这仅适用于包含这种机制的软件包。
猜你喜欢
  • 1970-01-01
  • 2017-08-13
  • 2018-01-27
  • 2018-06-17
  • 1970-01-01
  • 1970-01-01
  • 2020-10-30
  • 2021-10-12
  • 2021-11-23
相关资源
最近更新 更多