$cs.Shell_ExecuteWithTimeout
Description
Section titled “Description”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.
Syntax
Section titled “Syntax”$cs.Shell_ExecuteWithTimeout(string command, string arguments = “”, bool mustexist = false, int timeout = 30)
Parameters
Section titled “Parameters”command (String)
Section titled “command (String)”Command to execute.
arguments (String)
Section titled “arguments (String)”Arguments for the command. Default is empty.
mustexist (Boolean)
Section titled “mustexist (Boolean)”Check that command exists before executing; returns 5 without running anything if it doesn’t. Default is $false.
timeout (Int32)
Section titled “timeout (Int32)”Seconds to wait before the process is forcibly killed. Default is 30.
Return value
Section titled “Return value”Integer. The process’s own exit code if it completes in time, or 0 if it had to be killed after the timeout expired.
Example
Section titled “Example”$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:
$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"}