Retrieving MSIEXEC exit code in PowerShell - powershell

I need to run an MSIEXEC command line from a PowerShell and check whether the installation was successful or not.
If I do:
msiexec.exe /qn /l*v e:/tmp/surfaceruntime.log /i '\\nas\lui\tools\surfaceruntime2.msi'
(where the specified MSI doesn't exist – that's for testing purposes)
I get a $LASTEXITCODE of 1
OTOH, if I do:
$parms=#("/qn", "/l*v", "e:/tmp/surfaceruntime.log";"/i";"\\nas\lui\tools\surfaceruntime2.msi")
$run=[System.Diagnostics.Process]::Start("msiexec",$parms)
$run.WaitForExit()
$run.ExitCode
I get 1619 (same as %ERRORLEVEL% if I run the command line from CMD).
How come $LASTEXITCODE is incorrect?

Try this:
(Start-Process -FilePath msiexec.exe -ArgumentList $parms -Wait -Passthru).ExitCode

Related

Powershell script Argumentlist not working correctly

I am trying to install a rather oldish application. I am wanting to do it in powershell because I have post install functions that need to be done in powershell
The -argumentlist is not starting all the arguments after the /qn ( silent mode install )
The application starts and proceeds to install unattended in silent mode but ignores all the arguments after /qn. So its installing without any of the parameters I have specified. Is there anything I am doing wrong? Could just be my quotes are in the wrong places. My powershell knowledge is very very rusty
$workingDirectory = (split-path $myinvocation.mycommand.path -parent)
#Installs my application
Start-Process -Filepath "$workingDirectory\application.exe" -ArgumentList "/args /qn reboot=reallysuppress SILENT=yes INSTALLSTANDALONE=0 CENTRALSERVERHOSTNAME=servername CENTRALSERVERPORT=1234 CSUSER=userName /L*V C:\Windows\Temp\install.log"
I'm assuming you ran the .EXE directly with those args to verify their correctness?
If that's the case, try this:
Start-Process -Filepath "$workingDirectory\application.exe" -ArgumentList "/args", "/qn", "reboot=reallysuppress", "SILENT=yes", "INSTALLSTANDALONE=0", "CENTRALSERVERHOSTNAME=servername", "CENTRALSERVERPORT=1234", "CSUSER=userName", "/L*V", "C:\Windows\Temp\install.log"
Or this:
Start-Process -Filepath "$workingDirectory\application.exe" -ArgumentList #("/args", "/qn", "reboot=reallysuppress", "SILENT=yes", "INSTALLSTANDALONE=0", "CENTRALSERVERHOSTNAME=servername", "CENTRALSERVERPORT=1234", "CSUSER=userName", "/L*V", "C:\Windows\Temp\install.log")
Or this:
& "$workingDirectory\application.exe" /args /qn reboot=reallysuppress SILENT=yes INSTALLSTANDALONE=0 CENTRALSERVERHOSTNAME=servername CENTRALSERVERPORT=1234 CSUSER=userName /L*V C:\Windows\Temp\install.log
Alternatively, what happens when you remove the /qn arg?
I ran into a similar issue.
Changed this:
& "$workingDirectory\application.exe" /args /qn reboot=reallysuppress SILENT=yes INSTALLSTANDALONE=0 CENTRALSERVERHOSTNAME=servername CENTRALSERVERPORT=1234 CSUSER=userName /L*V C:\Windows\Temp\install.log
To this:
& "$workingDirectory\application.exe" /qn reboot=reallysuppress SILENT=yes INSTALLSTANDALONE=0 CENTRALSERVERHOSTNAME=servername CENTRALSERVERPORT=1234 CSUSER=userName /L*V C:\Windows\Temp\install.log
Source: https://social.technet.microsoft.com/wiki/contents/articles/7703.powershell-running-executables.aspx

ExitCode 1603 when silently installing a .msi

I'm trying to write a script that installs a .msi silently. When I run the command from the Powershell command line as a ./thing.msi with the argument /qn, it works just fine.
However, now that it is in a script it is returning a 1603 error ("A fatal error occurred during install"). If I try to switch it up and go to /qb with or without /quite, it runs, but it's not silent. Using -WindowStyle Hidden is doing nothing of note either. Any thoughts?
$InsightInstall = Start-Process -FilePath $PSScriptRoot\support.msi -
ArgumentList "/quiet /qb" -Wait -Passthru -WindowStyle Hidden
if($InsightInstall.ExitCode -eq 0)
{
Write-Host "Installation complete."
}
else
{
Write-Host "Failed with ExitCode" $InsightInstall.ExitCode
pause
}
You don't need to try that hard (I don't think Start-Process is needed). Just run msiexec and specify the package, followed by parameters.
msiexec /i d:\path\package.msi /quiet

Power Shell Invoking an MSI

I am trying to run an msi installer file using powershell. Below is my power shell code:-
$argumentlist = "/i D:\FolderTest\InstallerTest 1.9.0.39621 Setup.msi /qn /l*v D:\FolderTest\InstallLog.log"
Start-Process -FilePath "C:\Windows\System32\msiexec.exe" -ArgumentList $argumentlist
Every time I try to run this code though The Windows installer appears telling me that the argumentList variable isnt set correctly. Can anybody tell me what the problem is with this code?
I think that spaces in the msi filename are what's preventing the msiexec to work properly. Try something like:
$argumentlist = "/i 'D:\FolderTest\InstallerTest 1.9.0.39621 Setup.msi' /qn /l*v D:\FolderTest\InstallLog.log"
PowerShell is a shell. It's designed to run commands you type. You don't need to use Start-Process. Just type the command and press Enter.
PS C:\> msiexec /i "D:\FolderTest\InstallerTest 1.9.0.39621 Setup.msi" /qn /l*v "D:\FolderTest\InstallLog.log"
As with any command, if a parameter contains spaces, enclose it in quotes.

How to call .cmd file as administrator?

Please let me know how to call .cmd file as administrator from PowerShell script:
The second line below should open as Administrator from a PowerShell script:
Set-Location "C:\client\service"
Invoke-Item "C:\client\service\_install.cmd"
Then the command prompt should wait after execution. This needs to handle in PowerShell script not possible to write in _install.cmd file.
Batch-scripts runs in CMD.exe, so you need to start a CMD.exe process as admin.
Start-Process -FilePath "C:\Windows\System32\cmd.exe" -ArgumentList "/k","C:\client\service\_install.cmd" -Verb RunAs -Wait
Start-Process is the cmdlet to start a process
-FilePath "C:\Windows\System32\cmd.exe" starts cmd.exe process
-ArgumentList "/k","C:\client\service\_install.cmd" tells cmd to leave the console open after running the script (is this what you wanted? if not, replace with /c so the cmd-window will close when done). The second argument is your script.
-Verb RunAs tells Start-Process to start the process as admin (you will recieve a UAC-window if enabled)
-Wait tells Start-Process to wait until the process is finished. With cmd /k this means after you exited the command prompt. If you've changed that to cmd /c, then it waits until the script is done.
If you need to change the working directory inside the cmd-file, then you need to modify the .cmd, or write a wrapper-script, like:
#echo off
cd /d C:\client\service
C:\client\service\_install.cmd

PowerShell execute external command in same window

I am trying to execute an external command using powershell, without having the second program to popup, I need to execute this program within the same PowerShell window and output both the log and the errors.
I started with this:
$outcome = Start-Process -Wait -FilePath "cmd.exe" -ArgumentList "dir" -NoNewWindow 2>&1
$outcome
But it doesn't work as expected. I still see the new window popping up with DOS and no redirect at all about the output, the errors and so on.
Am I doing something wrong?
Does this work for you?
$outcome = Invoke-Expression "cmd.exe /c dir"
$outcome
My suggestion is to use the call operator
& cmd.exe /c dir
You can just run it. You're missing "/c".
$outcome = cmd /c dir
With the added complication of start-process, you'd have to save the output to a file.
Start-Process -Wait -FilePath "cmd.exe" -ArgumentList "/c","dir" -NoNewWindow 2>&1 -RedirectStandardOutput cmd.log