Powershell 使用参数调用 MSI [英] Powershell Call MSI with Arguments

查看:31
本文介绍了Powershell 使用参数调用 MSI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写用于安装 Autodesk 产品的 powershell 脚本,但遇到了一些问题.

I'm working on a powershell script to install Autodesk products and I am having some trouble.

我尝试了很多不同的方法,但总是遇到错误.

I've tried this many different ways and keep running into errors.

使用双引号

(Start-Process "msiexec.exe" -ArgumentList ""/i $dirFiles\ABDS2017\Img\x64\RVT\RVT.msi INSTALLDIR="C:\Program Files\Autodesk\" ADSK_SETUP_EXE=1 /qb!"" -NoNewWindow -Wait -PassThru).ExitCode

错误无法验证参数ArgumentList"上的参数参数为空或为空.

Error Cannot validate argument on parameter 'ArgumentList' Argument is null or empty.

使用变量定义InstallDir

Using a variable to define InstallDir

$RevitInstallDir = "C:\Program Files\Autodesk\"
    (Start-Process "msiexec.exe" -ArgumentList "/i $dirFiles\ABDS2017\Img\x64\RVT\RVT.msi INSTALLDIR=$RevitInstallDir ADSK_SETUP_EXE=1 /qb!" -NoNewWindow -Wait -PassThru).ExitCode

这样做我得到了 msiexec/option 必需参数错误.

Doing this I get the msiexec /option Required Parameter error.

也试过这个,单引号和路径上的引号

Tried this also, Single Quotes with Quotes on Path

(Start-Process "msiexec.exe" -ArgumentList "/i $dirFiles\ABDS2017\Img\x64\RVT\RVT.msi INSTALLDIR="C:\Program Files\Autodesk\" ADSK_SETUP_EXE=1 /qb!" -NoNewWindow -Wait -PassThru).ExitCode

我收到无法找到接受参数 C:\Program 的位置参数

I receive A positional parameter cannot be found that accepts argument C:\Program

在 InstallDir 上使用单引号

Using single quotes on InstallDir

(Start-Process "msiexec.exe" -ArgumentList "/i $dirFiles\ABDS2017\Img\x64\RVT\RVT.msi INSTALLDIR='C:\Program Files\Autodesk\' ADSK_SETUP_EXE=1 /qb!" -NoNewWindow -Wait -PassThru).ExitCode

这样做我得到了 msiexec/option 必需参数错误.

Doing this I get the msiexec /option Required Parameter error.

在外面使用单引号

(Start-Process "msiexec.exe" -ArgumentList '/i $dirFiles\ABDS2017\Img\x64\RVT\RVT.msi INSTALLDIR="C:\Program Files\Autodesk\" ADSK_SETUP_EXE=1 /qb!' -NoNewWindow -Wait -PassThru).ExitCode

如果我这样做,它会阻止 $dirFiles 变量工作.

If I do this, it prevents the $dirFiles variable from working.

我使用 Start-Process 的原因是因为我有很多安装程序一个接一个,我希望一个安装程序在完成之前等待一个安装程序.任何帮助,将不胜感激!谢谢

The reason I'm using Start-Process is because I have many installers one after the other and I want one installer to wait till the one before it finishes. Any help would be appreciated! Thanks

没关系,我想通了.

(Start-Process "msiexec.exe" -ArgumentList "/i $dirFiles\ABDS2017\Img\x64\RVT\RVT.msi INSTALLDIR=""C:\Program Files\Autodesk\"" ADSK_SETUP_EXE=1 /qb!" -NoNewWindow -Wait -PassThru).ExitCode

安装目录周围的双引号

从这里得到了这个想法.https:///blogs.technet.microsoft.com/heyscriptingguy/2015/06/20/weekend-scripter-understanding-quotation-marks-in-powershell/

Got the idea from here. https://blogs.technet.microsoft.com/heyscriptingguy/2015/06/20/weekend-scripter-understanding-quotation-marks-in-powershell/

谢谢

推荐答案

不要打扰 Start-Process,除非您需要以提升的权限运行进程.使用呼叫运算符splatting 代替.命令的退出代码存储在 自动变量 $LastExitCode.

Don't bother with Start-Process unless you need to run a process with elevated privileges. Use the call operator and splatting instead. The exit code of the command is stored in the automatic variable $LastExitCode.

$params = '/i', "$dirFiles\ABDS2017\Img\x64\RVT\RVT.msi",
          'INSTALLDIR="C:\Program Files\Autodesk"', 'ADSK_SETUP_EXE=1',
          '/qb!'
& msiexec.exe @params
$LastExitCode

不幸的是,您不能告诉 msiexec.exe 等待安装完成,并且调用运算符也不强制同步执行.如果您需要等待安装完成后再继续,您需要像 CMD 内置的 start 命令或 Start-Process 之类的东西.不过,我仍然建议将参数定义为数组.

Unfortunately you cannot tell msiexec.exe to wait for an installation to complete, and the call operator also doesn't enforce synchronous execution. If you need to wait for the installation to complete before proceeding you need something like the CMD-builtin start command or Start-Process. I would still recommend defining the parameters as an array, though.

$params = '/i', "$dirFiles\ABDS2017\Img\x64\RVT\RVT.msi",
          'INSTALLDIR="C:\Program Files\Autodesk"', 'ADSK_SETUP_EXE=1',
          '/qb!'
$p = Start-Process 'msiexec.exe' -ArgumentList $params -NoNewWindow -Wait -PassThru
$p.ExitCode

这篇关于Powershell 使用参数调用 MSI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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