首页 > 解决方案 > 使用 Powershell 从列表中安装软件

问题描述

我正在构建一个脚本来自动化计算机构建和配置:这个想法是 WDS 尽可能干净,自动运行这个脚本,它将检查序列号,查询我们的 Workday 资产数据库并根据什么配置操作系统分配给该系统需求的用户。

现在我专注于 3 个大组:笔记本电脑、台式机和实验室。所有 3 将有一些 SW 将是相同的,一些将是特定的。我的问题是 msiexec:最初,我对每个组的所有安装进行了硬编码。但这意味着每次更新某些内容时我都必须更改脚本(例如默认推出一个新应用程序)。这并不理想。

    function Install-Desktop {
    #Write-Output "Here will be the install Desktop computer script"
    $IPATH="<Path To root sw folder>"
    #Software List
    <#          SOFTWARE LIST           #>
    $office="$IPATH\script\o365"
    $webex="$IPATH\script\webex"
    $chrome="$IPATH\script\chrome"
    #install Ofice:
    Invoke-Expression "$office\setup.exe /configure $office\O365.xml"
    $params = '/i', "$webex\webexapp.msi",'/qb!','/norestart'
    Start-Process msiexec -ArgumentList "$params"  -Wait -PassThru
    $params = '/i', "$chrome\GoogleChromeStandaloneEnterprise64.msi",'/qb!','/norestart'
    Start-Process msiexec -ArgumentList $params  -Wait -PassThru
    }

这段代码运行良好。

现在我的想法是从列表中导入要安装的软件(维护列表比每次修改脚本更容易)。就像是:

function install-software {
    param (
        [String]$Type
    )
    $IPATH=<ROOT SW Folder>
    $SoftWares=Import-Csv -Path "$IPath\script\$Type`.csv" #there will be a Laptop.csv in that path
    foreach ($Software in $SoftWares) {
        #detect if it is msiexect or other:
        # (this has to do with how the csv is built, the first parameter is '/i' if it is an msi installer)
        if ($Software.param1 -eq "'/i'") {
            Start-Process msiexec -ArgumentList $Software  -Wait -PassThru
        }
        else {
            $Params=[string]::Join(" ",$Software.param1,$Software.param2,$Software.param3,$Software.param4)
            Invoke-Expression "$Params" 
        }
    }
}

这仅适用于其他部分。但是在 if 的 msiexec 端,MSI 打开时不带参数。我尝试了很多方法来传递参数,但都没有奏效。无论如何,我都不是 PowerShell 专家,所以我可能在这里缺少一些东西。

标签: powershellautomationwindows-installer

解决方案


好吧,看起来你必须通过完整路径,它甚至不允许你使用挂载的网络驱动器:所以答案在 csv 上。而不是 S:\<安装程序的路径> 它必须是 \<安装程序的完整路径> 并且我还必须去掉所有的引号和双引号。


推荐阅读