首页 > 解决方案 > 将 msiexec 命令从 cmd 转换为 powershell

问题描述

在我的powershell脚本中,我需要msiexec安静地运行几个命令。问题是当我尝试运行命令时,Windows Installer 帮助弹出窗口显示而不是执行命令。(下图)

在此处输入图像描述

相同的命令在 cmd 中运行良好。下面是我的命令。我&将命令中的 in 保留在双引号中,以按照建议将其视为字符串。

& msiexec /log c:\msxml.log /quiet /I "&" D:\LoadGeneratorsetup\prerequisites\msxml6\msxml6_x64.msi

我尝试使用Start-Process -FilePath它来运行它,但最终出现以下错误。

Start-Process : A positional parameter cannot be found that accepts argument 'c:\msxml.log'.
At line:1 char:1
+ Start-Process -FilePath msiexec /log c:\msxml.log /quiet /I "&" D:\Lo ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Start-Process], ParameterBindingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.StartProcessCommand

有人可以提供有关如何使用 powershell 安静地执行命令的详细信息。

标签: powershellscriptingwindows-installer

解决方案


对于您的第二个命令:

& msiexec /i "& D:\LoadGeneratorsetup\HP_LoadGenerator.msi" /qb /l*vx "& D:\LoadGeneratorsetup\Logs\InstallationLogs"+"_"+(Get-Date -Format "yyyy-MM-dd-hh-mm-s")+".txt"

您有两个选择,要么将日志路径设置为变量,要么只将路径括起来:

1 - 设置为变量

$logfile = "D:\LoadGeneratorsetup\Logs\InstallationLogs" + "_" + (Get-Date -Format "yyyy-MM-dd-hh-mm-s") + ".txt"
msiexec /i "D:\LoadGeneratorsetup\HP_LoadGenerator.msi" /qb /l*vx $logfile

2 - 将路径括起来

msiexec /i "D:\LoadGeneratorsetup\HP_LoadGenerator.msi" /qb /l*vx ("D:\LoadGeneratorsetup\Logs\InstallationLogs" + "_" + (Get-Date -Format "yyyy-MM-dd-hh-mm-s") + ".txt")

我假设该命令只是在运行命令之前不评估日志路径。


推荐阅读