我正在尝试使用 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
您创建数组的尝试将无法按预期工作:
$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.
}