running msiexec with parameters on remote machine

180 Views Asked by At

I am trying to run msiexec.exe with powershell on remote machine using Invoke-Command but it is not passing arguments into installer. I tried in several ways, my code looks like this:

$params = @(
  $DataPath = 'C:\Program Files (x86)\path'
  $Type = "Dev"
  $DbUser="User"
  $DbPassword="password"
  $sqlServerUser= "user"
  $sqlServerPassword="password"
  $QMName="name"
  $QMAddress="address"
  $QMPort="1212"
  $MqUser="user"
  $MqPassword="password"
  $SERVER_TYPE="Dev" 
  $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

AND

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

I have tried: Install MSI on remote host using powershell

Installing an MSI file on a remote machine with PowerShell

Install .msi remotely using Powershell

1

There are 1 best solutions below

1
mklement0 On

Your attempt to create an array won't work as intended:

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

This is the same as $params = @(), i.e. creating an empty array, and creating individual variables named $DataPath, $Type, ...; that is, your use of @(...), the array-subexpression operator encloses variable assignments, which themselves output nothing, so that the resulting array is empty.

If the intent is to define $params as an array of CLI arguments for msiexec, specifically as PROPNAME=PROPVALUE arguments, you need to use this instead:

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

Note the use of literal property names, the need to quote each argument (line) as whole, the absence of whitespace around =, and the embedded, selective "..." quoting for property values that contain spaces.

Then you should be able to call as follows:

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:installerPath`" $using:params"
  # $LASTEXITCODE now contains msiexec's exit code.
}