在文件夹中安装所有 .msi 文件 [英] Installing all .msi files within a folder

查看:32
本文介绍了在文件夹中安装所有 .msi 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个从 python 脚本开始的 powershell 脚本,该脚本将允许我浏览并安装包含在某个文件夹中的每个 .msi 文件.

这是我目前所拥有的:

$msiFiles = Get-ChildItem -Path "***PATH TO FOLDER***" -Recurse -Include *.msiforeach($msiFiles 中的 $file){Write-Host "$file 正在安装"启动进程 "msiexec.exe" -arg "/I $file/qb ADDLOCAL=ALL ALLUSERS=TRUE" -WaitWrite-Host "$file 安装完成"}

我想以静默模式安装这些 msi,所以我看不到或不需要 ant GUI 交互.当它开始安装 msi 时,我会从安装程序中弹出一个包含所有可能用于安装东西的命令,但它实际上并没有安装它.

在静默模式下安装这些的正确命令是什么?

解决方案

通常,如果调用 msiexec 只会显示一个描述命令行语法的对话框,这意味着语法问题.

语法错误的可能来源是您用作 -arg 参数的 "..." 字符串 (全名:-Args-ArgumentList) 中嵌入了 $file 没有嵌入引用:

也就是说,如果$file 的值嵌入了空格,例如(例如,C:\Msi Installers\foo.msi),生成的 msiexec 命令在语法上将是无效的,因为路径的空格分隔标记每个都被视为一个参数.>

Bill_Stewart 的有用回答 向您展示了如何在 使用嵌入式引用>$file 来解决这个问题,通过将它包含在 `" 中(` 是 PowerShell 的转义字符).

如果您坚持将参数作为单个字符串传递,您可以使用:

Start-Process msiexec.exe -Args "/I `"$file`"/qb ADDLOCAL=ALL ALLUSERS=TRUE" -Wait

然而,可以说,更简洁不是将单个、类似命令行的字符串作为唯一参数传递,而是改为将参数作为数组的元素传递,这确实是 -ArgumentList/-Args 被设计为接受的(它的参数类型是[string[]]):

Start-Process msiexec.exe -Args/I, `"$file`",/qb, ADDLOCAL=ALL, ALLUSERS=TRUE -Wait

注意$file是如何仍然通过嵌入式引用传递的,不幸的是,由于Start-Process 中的一个错误(从 Windows PowerShell v5.1/PowerShell 核心 v6.1.0)

<小时>

您也可以提前构建参数数组;注意这是如何在表达式模式下完成的(语法更像常规编程语言),因此数组元素all需要引用;还要注意我是如何使用引号来定义文字参数的:

# 创建要传递给 msiexec 的参数数组$msiArgs ='/一世',"`"$file`"", #`# !!由于上面提到的错误,需要用 `"...`" 括起来'/qb','/ADDLOCAL=ALL','所有用户=真'启动进程 msiexec.exe -Args $msiArgs -Wait

I'm trying to write a powershell script that I am kicking off from a python script that will allow me to go through and install each .msi file that is contained within a certain folder.

This is what I have so far:

$msiFiles = Get-ChildItem -Path "***PATH TO FOLDER***" -Recurse -Include *.msi

foreach($file in $msiFiles)
{

    Write-Host "$file is being installed"
    Start-Process "msiexec.exe" -arg "/I $file /qb ADDLOCAL=ALL ALLUSERS=TRUE" -Wait
    Write-Host "$file is finished being installed"


}

I want to install these msi's in silent mode so I don't see or need ant GUI interaction. When it goes to install the msi's I get a pop up from the installer with all the possible commands to use to install stuff, but it doesn't actually install it.

What is the proper command to install these in silent mode?

解决方案

Generally, if invoking msiexec does nothing but show a dialog describing the command-line syntax, the implication is that there's a syntax problem.

The likely source of the syntax error is that the "..." string you're using as the -arg argument (full name: -Args or -ArgumentList) has $file embedded in it without embedded quoting:

That is, if the value of $file has embedded spaces, for instance (e.g., C:\Msi Installers\foo.msi), the resulting msiexec command will be syntactically invalid, because the space-separated tokens of the path are each considered an argument.

Bill_Stewart's helpful answer shows you how to use embedded quoting around $file to solve this problem, by enclosing it in `" (` is PowerShell's escape character).

If you were to stick with passing the arguments as a single string, you would use:

Start-Process msiexec.exe -Args "/I `"$file`" /qb ADDLOCAL=ALL ALLUSERS=TRUE" -Wait

Arguably, however, it's cleaner not to pass a single, command-line-like string as the only argument, but to instead pass the arguments as elements of an array, which is indeed what -ArgumentList / -Args was designed to accept (its parameter type is [string[]]):

Start-Process msiexec.exe -Args /I, `"$file`", /qb, ADDLOCAL=ALL, ALLUSERS=TRUE -Wait

Note how $file is still passed with embedded quoting, which is unfortunately required due to a bug in Start-Process (as of Windows PowerShell v5.1 / PowerShell Core v6.1.0)


You may alternatively build up the array of arguments in advance; note how this is done in expression mode (with syntax more like regular programming languages), so the array elements all require quoting; also note how I'm using single quotes to define literal arguments:

# Create the array of arguments to pass to msiexec
$msiArgs = 
  '/I',
  "`"$file`"",      #`# !! enclosing in `"...`" is needed due to the bug mentioned above
  '/qb',
  '/ADDLOCAL=ALL',
  'ALLUSERS=TRUE'

Start-Process msiexec.exe -Args $msiArgs -Wait      

这篇关于在文件夹中安装所有 .msi 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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