如何运行 Windows 安装程序并在 PowerShell 中获得成功/失败值? [英] How do I run a Windows installer and get a succeed/fail value in PowerShell?
问题描述
我想安装一组应用程序:.NET 4、IIS 7 PowerShell 管理单元,ASP.NET MVCa> 3 等.如何让应用程序安装并返回一个确定安装是否成功的值?
I would like to install a set of applications: .NET 4, IIS 7 PowerShell snap-ins, ASP.NET MVC 3, etc. How do I get the applications to install and return a value that determines if the installation was successful or not?
推荐答案
这些答案似乎要么过于复杂,要么不够完整.在 PowerShell 控制台中运行安装程序存在一些问题.MSI 在 Windows 子系统中运行,所以你不能只调用它们(Invoke-Expression
或 &
).有些人声称通过管道传递到 Out-Null
或 Out-Host
来使这些命令起作用,但我没有观察到它起作用.
These answers all seem either overly complicated or not complete enough. Running an installer in the PowerShell console has a few problems. An MSI is run in the Windows subsystem, so you can't just invoke them (Invoke-Expression
or &
). Some people claim to get those commands to work by piping to Out-Null
or Out-Host
, but I have not observed that to work.
对我有用的方法是 Start-Process
将静默安装参数设置为 msiexec
.
The method that works for me is Start-Process
with the silent installation parameters to msiexec
.
$list =
@(
"/I `"$msi`"", # Install this MSI
"/QN", # Quietly, without a UI
"/L*V `"$ENV:TEMP\$name.log`"" # Verbose output to this log
)
Start-Process -FilePath "msiexec" -ArgumentList $list -Wait
您可以获得退出代码 来自 Start-Process
命令并检查它的通过/失败值.(这里是退出代码参考)
You can get the exit code from the Start-Process
command and inspect it for pass/fail values. (and here is the exit code reference)
$p = Start-Process -FilePath "msiexec" -ArgumentList $list -Wait -PassThru
if($p.ExitCode -ne 0)
{
throw "Installation process returned error code: $($p.ExitCode)"
}
这篇关于如何运行 Windows 安装程序并在 PowerShell 中获得成功/失败值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!