Sometimes I want to run older programs / tools like MSIEXEC or such to run from within my Powershell scripts or modules. I wanted to make it easy for myself to format the exact command line, and capturing non-standard output that is written to the screen .
I wrote this simple function to start any .exe or other w32 process, wait for it to complete, and capturing both the exit code and the full output in an array while the window remains hidden.
Example call to this function:
$res = runProcess msiexec "/i AcroRdrDC1500720033_nl_NL.msi /quiet"
write-host "Result code of msiexec: $($res[0])"
write-host "All output of msiexec: $($res[1])"
And here is the function:
function runProcess ($cmd, $params, $windowStyle=1) {
$p = new-object System.Diagnostics.Process
$p.StartInfo = new-object System.Diagnostics.ProcessStartInfo
$exitcode = $false
$p.StartInfo.FileName = $cmd
$p.StartInfo.Arguments = $params
$p.StartInfo.UseShellExecute = $False
$p.StartInfo.RedirectStandardError = $True
$p.StartInfo.RedirectStandardOutput = $True
$p.StartInfo.WindowStyle = $windowStyle; #1 = hidden, 2 =maximized, 3=minimized, 4=normal
$null = $p.Start()
$output = $p.StandardOutput.ReadToEnd()
$exitcode = $p.ExitCode
$p.Dispose()
$exitcode
$output
}