【问题标题】:PowerShell script for software installation using chocolatey使用 Chocolatey 安装软件的 PowerShell 脚本
【发布时间】:2021-09-01 10:37:18
【问题描述】:
    enter code here
# 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"
}

问题陈述: 我正在使用巧克力编写 power shell 脚本以自动下载和安装软件。每当用户输入相应软件将下载和安装的编号时,我的脚本都会显示一个包含软件列表的菜单。 当我运行脚本时,可执行文件未显示在控制面板中,并且桌面图标也未创建,但我在 power-shell 终端中收到该软件已安装但我无法看到该应用程序的消息。 我需要在我的脚本中添加一些条件:

  1. 如果该软件已安装,则不应下载该软件
  2. 安装软件后,它必须显示在控制面板中,并且应创建桌面图标。
  3. 如果对 googlechrome 和 firefox 进行任何升级,通常会发生向上渐变,则必须升级脚本。 如果有任何问题,请帮助我并编辑我的脚本

提前致谢

【问题讨论】:

  • 您是在用户的上下文中运行它,还是在某些有权实际触摸所有路径的服务用户的上下文中运行?
  • 正如我 commented yesterday 你需要研究巧克力开关和配置文件,所以 choco.exe 会知道你想要安装/更新什么,你想要那个软件的什么功能等等。如果如果您想让 choco 进行安装,您需要指示 it 如何进行安装。您还可以使用纯 PowerShell 安装所有内容,然后我给您的代码应该可以处理所有这些。在这种情况下,您正在调用外部应用程序来完成这项工作..
  • 不,先生,我不是这个脚本的新手,这就是我无法做到这一点的原因。无论您给出什么代码,我都理解,但我无法说出逻辑,所以我面临困难,这就是为什么我反复询问,因为我必须提交作业对不起先生。
  • 听起来你是在要求别人为你完成你的剧本。这不是 Stack Overflow 的意义所在,提出这样的要求可能会让您的问题结束。我已经提供了关于如何使用 Chocolatey 实现您想要的一般建议的答案,但 Stack Overflow 不是代码编写服务。
  • 我在运行我的脚本时收到以下消息:软件安装为“MSI”,安装位置可能是默认的,但它没有显示在控制面板中,当我尝试使用 catch 块进行检查时如果软件已经安装或没有,在我的情况下,软件已经安装,那么软件也在下载。我将发布我的脚本 请提供有关此问题的信息

标签: powershell user-interface package chocolatey


【解决方案1】:

Referencing this answer,通过检查以下注册表项检查是否安装了相关软件:

$InstalledSoftware = Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*
$InstalledSoftware += Get-ItemProperty HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*

如果已安装,您可以完全跳过安装,或者...

无论如何,您都可以通过“安装”包来获得在 Chocolatey 下管理的包,但需要提供-n (--skippowershell) 参数:

choco install -n packageName

这将下载包元数据,但跳过实际的 PowerShell 代码(chocolateyInstall.ps1,嵌入到所有 Chocolatey 包中)。这是运行 MSIEXEC(一个 EXE 安装程序)的部分,在没有适当安装程序的情况下为软件包提取 zip 存档等。这有利于让 Chocolatey 管理未来的更新,而无需重新安装已经存在的软件。

注意:您必须弄清楚特定软件包的一些细微差别,但对于巧克力包中的大多数可用内容,这种方法都可以使用。

【讨论】:

    猜你喜欢
    • 2018-06-17
    • 2018-01-27
    • 1970-01-01
    • 2021-10-12
    • 1970-01-01
    • 1970-01-01
    • 2018-02-23
    • 1970-01-01
    • 2022-10-13
    相关资源
    最近更新 更多