Install MSI packages with Powershell 2 on Win 2008

214 Views Asked by At

My Powershell 2.0 script is installing an MSI package using something like the following excerpt.

(...)
msiexec.exe /norestart /qn /passive `
        /l*v "$Dir\Install.log" `
        /i "$dirTemp\$msi" `
        INSTALLFOLDER="$Dir"
(...)

It runs successfully, but I would like the script to wait for the installation to end before proceeding to the next instructions. Is there a way to force the script to wait msiexec to finish?

20220909 EDIT: Tried suggested answer ⬇️

As suggested, I've tried Start-Process as shown bellow.

$Dir = "c:\test"
$dirTemp = $env:TEMP
$msi = "package.msi"
$proxy = "1.2.3.4"
$metadata = "value"

$argList = @('/norestart',
            '/qn',
            '/passive',
            '/l*v',
            "$Dir\Install.log",
            '/i',
            "$dirTemp\$msi",
            "INSTALLFOLDER='$Dir'",
            "LOGFILE='$Dir\Agent.log'",
            "SERVER='$proxy'",
            "BUFFERFILE='$Dir\Agent.db'",
            "METADATA='windows $metadata'")
Start-Process msiexec.exe -ArgumentList "$argList" -Wait -PassThru

By using Start-Process, Windows Installer fails to start and only shows me the msiexec "help" window.

enter image description here

I could not find a syntax error. Also, there's no logs to be found at the appointed log directory.

Thank you

20220910 EDIT: Found the issue ⬇️

I had to change the single quotation marks from one of the parameters to a double quotation and scape them.

"METADATA=`"windows $metadata`""

It seems msiexec did not like the space inside the parameter's value.

2

There are 2 best solutions below

1
Toni On BEST ANSWER

PS2 also has the cmdlet start-process if I remember correctly. There you can specify the parameter "-wait":

$argumentList = @(
    '/norestart'
    '/qn'
    '/passive'
    '/l*v'
    "$Dir\Install.log"
    '/i' 
    "$dirTemp\$msi"
    "INSTALLFOLDER='$Dir'"
)

$result = start-process msiexec.exe -ArgumentList $argumentList -Wait -PassThru
0
Ranadip Dutta On

Since Windows Installer 1.0 was first released, msiexec.exe has always run in the Windows subsystem. That means that when it is executed from the console or by a batch script control returns to the console or script immediately.

In this scenario I like to use start /wait from the command line or a batch script. This will create the process and wait for it to exit, and the return code from the process is passed through and returned from the start command such that %ERRORLEVEL% is set accordingly.

start /wait msiexec.exe followed by your remaining parameter