在远程计算机上运行带参数的 msiexec

问题描述 投票:0回答:1

我正在尝试使用 Invoke-Command 在远程计算机上使用 powershell 运行 msiexec.exe,但它没有将参数传递到安装程序中。我尝试了多种方法,我的代码如下所示:

$参数=@(

$DataPath = 'C:\Program Files (x86)\path'

$类型=“开发”

$DbUser="用户"

$DbPassword="密码"

$sqlServerUser=“用户”

$sqlServerPassword="密码"

$QMName="名称"

$QMAddress="地址"

$QMPort="1212"

$MqUser="用户"

$MqPassword="密码"

$SERVER_TYPE="开发"

$RUNTIME_SQL_AUTH_TYPE =“WINDOWS_AUTH”

$SQL_AUTH_TYPE="WINDOWS_AUTH"

$RECYCLING_TIME="03:00")

$InstallerPath =“$Path.msi”

$scriptInstallServer = { (Start-Process -FilePath "msiexec.exe" -ArgumentList "/i $Using:InstallerPath /qn /norestart $Using:params" -Wait -Passthru).WaitForExit() }

Invoke-Command -ComputerName $server -Credential $cred -ScriptBlock $scriptInstallServer

并且

Invoke-Command -ComputerName $server -Credential $cred -ScriptBlock {msiexec /i $Using:serverInstallerPath $Using:params /passive}

我尝试过: 使用 powershell 在远程主机上安装 MSI

使用 PowerShell 在远程计算机上安装 MSI 文件

使用 Powershell 远程安装 .msi

powershell windows-installer invoke powershell-remoting
1个回答
0
投票

您创建数组的尝试将无法按预期工作:

$params = @(

  $DataPath = 'C:\Program Files (x86)\path'

  $Type = "Dev"

  # ...

)

这与

$params = @()
相同,即创建一个数组,并创建名为$DataPath
$Type
、...
单独

变量

如果意图将

$params
定义为
msiexec
的 CLI 参数数组,特别是
PROPNAME=PROPVALUE
参数,则需要使用此替代:

$params = @(
  'DataPath="C:\Program Files (x86)\path"'
  'Type=Dev'
  # ...
)

请注意,需要将每个参数(行)作为整体引用,

=
周围不存在空格,以及对于具有空格的属性值,嵌入、选择性
"..."
引用

那么您应该可以拨打以下电话:

Invoke-Command -ComputerName $server -Credential $cred -ScriptBlock {
  # cmd /c invokes msiexec *synchronously*
  # Passing the msiexec command line inside "..." overall ensures
  # that the selective value quoting is passed through as such.
  cmd /c "msiexec /qn /norestart /i `"$using:serverInstallerPath`" $using:params"
  # $LASTEXITCODE now contains msiexec's exit code.
}
© www.soinside.com 2019 - 2024. All rights reserved.