using msiexec in "if" statement

by rob.irvin at 2012-10-22 15:05:06

Hi. I am trying to install software with an “if” statement. Here’s a simple example:

if(msiexec /i “c:\setup.msi”)
{write-output “successful”}
{write-output “failed”}

Well, this doesn’t work and immediately returns “failed”.
I think I need to use the msiexec return code somehow but not sure about how to do that. I am still researching on the web but any help would be appreciated.

by DonJ at 2012-10-22 15:14:58
Consider using Start-Process to start it. That can return a Process object, which can provide access to the process’ exit code.
by rob.irvin at 2012-10-22 15:25:08
Thanks. I will need to look into this. After I posted I found something and it seems to work. What are your thoughts on this approach?

(msiexec /i “c:\setup.msi” |Out-Null)
if($lastexitcode -eq 0)
{Write-Output “installed”}
{Write-Output “not installed”}
by DonJ at 2012-10-22 15:30:23
That works, too, although you don’t technically need the parentheses. If you know that’ll be the only process running, then it’s certainly easier.
by rob.irvin at 2012-10-23 09:21:37
Cool thanks. But what do you mean when you say “if you know that’ll be the only process running”?

Thanks again