dcaz
dcaz

Reputation: 909

Wait-Process does not always work with Start-Process

$GAMCheck = invoke-command -ScriptBlock { C:\GAMADV-XTD3\GAM.exe version checkrc }

If ($GAMCheck) {
    $current = $GAMCheck.split(":")[19].Trim()
    $latest = $GAMCheck.split(":")[21].Trim()
    
    If ($LASTEXITCODE -eq 1) {  
        Try {
            $NeedUpGradeCode = $LASTEXITCODE
            $client = new-object System.Net.WebClient
            $client.DownloadFile("https://github.com/taers232c/GAMADV-XTD3/releases/download/v$latest/GAMadv-xtd3-$latest-windows-x86_64.msi", "C:\Temp\GAMadv-xtd3-$latest-windows-x86_64.msi")
            Start-Process -Filepath "C:\Temp\GAMadv-xtd3-$latest-windows-x86_64.msi" -ArgumentList "/passive" | Wait-Process -Timeout 75
            Remove-Item "C:\Temp\GAMadv-xtd3-$latest-windows-x86_64.msi"
  

            $GAMCheck = $null
            $GAMCheck = invoke-command -ScriptBlock { C:\GAMADV-XTD3\GAM.exe version checkrc }
            $newCurrent = $GAMCheck.split(":")[19].Trim()
    
            $resultsarray = [PSCustomObject]@{    
                CurrentVersion   = $current
                LatestVersion    = $latest
                NeedUpgradeCode  = $NeedUpGradeCode
                Upgraded         = $true
                NewCurrent       = $newCurrent
                AfterUpgradeCode = $LASTEXITCODE
            }
        }
        Catch {
            Write-Warning "Problem with site or command. Maybe go to https://github.com/taers232c/GAMADV-XTD3/releases and download the current GAM and then install GAM in C:\GAMADV-XTD3\ again"
        }
    }
}

lately I have been noticing that the | Wait-process 75 above is causing an error. If I run the command with out it everything is fine.

Is there another way to wait for the install ?

Upvotes: 1

Views: 573

Answers (1)

mklement0
mklement0

Reputation: 437458

To launch a process with Start-Process and wait for it to exit, use the -Wait switch.

Piping a Start-Process call to Wait-Process would only work as intended if you included the -PassThru switch, which makes Start-Process - which by default produces no output - emit a System.Diagnostics.Process instance representing the newly launched process, on whose termination Wait-Process then waits.

Note that, surprisingly, the behavior of these two seemingly equivalent approaches is not the same, as discussed in GitHub issue #15555.

Upvotes: 4

Related Questions