Skip to content

$cs.Service_Start

Starts the named service and waits up to maxTimeout milliseconds for it to reach the Running state. Throws an exception if the service doesn’t exist, and also throws a timeout-related exception if it doesn’t reach Running within maxTimeout.

$cs.Service_Start(string servicename, uint maxTimeout = 60000)

Name of the service to start.

Maximum time to wait, in milliseconds, for the service to reach the Running state. Defaults to 60000 (60 seconds).

None.

Check the service exists before starting it:

Terminal window
if ($cs.Service_Exist("gupdate"))
{
$cs.Job_WriteLog("Starting gupdate service")
$cs.Service_Start("gupdate")
}

Start a service right after installation and catch the exception if it fails or times out, without failing the whole install:

Terminal window
try
{
$cs.Job_WriteLog("Starting MyAppService after install")
$cs.Service_Start("MyAppService", 90000)
}
catch
{
$cs.Job_WriteLog("MyAppService failed to start within the timeout: $($_.Exception.Message)")
}

Start a service with a shorter timeout when it’s expected to come up quickly, letting the exception propagate to fail the job if it doesn’t:

Terminal window
$cs.Job_WriteLog("Starting lightweight helper service")
$cs.Service_Start("MyHelperService", 15000)

$cs.Service_Exist $cs.Service_Stop