Skip to content

$cs.Shell_ExecuteWithTimeout

Executes a command and waits for it, like $cs.Shell_Execute, but forcibly kills the process if it hasn’t exited within timeout seconds. The window is always hidden. Use this instead of $cs.Shell_Execute when a command could hang and you need a hard upper bound on how long the PowerPack waits for it.

$cs.Shell_ExecuteWithTimeout(string command, string arguments = “”, bool mustexist = false, int timeout = 30)

Command to execute.

Arguments for the command. Default is empty.

Check that command exists before executing; returns 5 without running anything if it doesn’t. Default is $false.

Seconds to wait before the process is forcibly killed. Default is 30.

Integer. The process’s own exit code if it completes in time, or 0 if it had to be killed after the timeout expired.

Terminal window
$MsiPath = Join-Path $global:Packageroot "kit" "GoogleChromeStandaloneEnterprise64.msi"
$Result = $cs.Shell_ExecuteWithTimeout("msiexec.exe", "/i `"$MsiPath`" /qn /norestart", $false, 120)
if ($Result -ne 0 -and $Result -ne 3010) {
$cs.Job_WriteLog("Chrome installation failed or timed out with exit code: $Result")
Exit-PSScript $Result
}

Running a vendor-supplied post-install script that has been known to hang, with mustexist=$true so a missing script doesn’t silently pass and a short timeout so the PowerPack never gets stuck:

Terminal window
$ScriptPath = Join-Path $global:Packageroot "kit" "PostConfig.cmd"
$Result = $cs.Shell_ExecuteWithTimeout($ScriptPath, "", $true, 60)
if ($Result -ne 0) {
Exit-PSScript -exitcode 1 -exitmessage "PostConfig.cmd failed or was killed after timeout, exit code: $Result"
}

$cs.Shell_Execute