Search code examples
powershelltimeoutwindows-installerstart-job

Powershell script - Start-Job & MSIEXEC


I wonder if you can help please? I have the need to write a Powershell script to execute an MSI script.

I also need to set a time out on the process (as the MSIs we're given sometimes hang).

I've seen that you can acheive this by using the Start-Job/ Wait-Job process

Obviously the code below in in a severe state of butchery currently

Thanks in advance

    $timeoutSeconds = 20

$uninstall32    = gci "HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall" | foreach { gp $_.PSPath } | ? { $_ -match "My File" } | select UninstallString$uninstall64    = gci "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall" | foreach { gp $_.PSPath } | ? { $_ -match "Vix.Cbe.SalesTransaction" } | select UninstallString
Echo  "uninstall32 :" $uninstall32

if ($uninstall32) {
$uninstall32 = $uninstall32.UninstallString -Replace "msiexec.exe","" -Replace "/I","" -Replace "/X",""
$uninstall32 = $uninstall32.Trim()
$32p = @("/X", "$uninstall32", "/b")
}
Echo  "uninstall32 :" $uninstall32
Echo  "u32 :" $32p


$32j = Start-Job msiexec  -ArgumentList $32p

if (Wait-Job $32j -Timeout $timeoutSeconds) { Receive-Process $32j }
Remove-Process -force $32j

Solution

  • You don't have to mess with jobs to do that. Here is the easy way:

    start Notepad -PassThru | select -ExpandProperty id | set id
    sleep 60
    kill $id -ea 0
    

    This might not work if app spawns another app and exits as the Id will be wrong. In that case you will probably have to hunt it in the process list or via cmd line params.