Invoke-RestartService
Description
Provide a script that will simultaneously restart multiple services and offer proper error handling.
Features
- Takes no action on disabled services.
- If a service cannot be stopped normally, the service's PID is obtained and used in a forced
taskkill
command. The results of that command are used to either restart the service if it's stopped or log an error.
Usage
- Start the restart service jobs if they are not disabled.
- Wait until all jobs finish.
- Get the job results:
- If successful, log success.
- If failure, get the PID and use a forced
taskkill
to attempt to stop the service.- If
taskkill
is successful, restart the service by name, then wait 60 seconds and check the state of the service.- If still running, log as success.
- If failed, log failure.
- If
taskkill
is not successful, log failure.
- If
- Remove any created jobs.
- Return the results.
.\Invoke-RestartService.ps1 -ServiceNames SAAZDPMACTL, SAAZWatchDog, SAAZServerPlus, SAAZScheduler
Restarts the SAAZDPMACTL, SAAZWatchDog, SAAZServerPlus, and SAAZScheduler services and verifies successful completion.
Parameters
Parameter | Alias | Required | Default | Type | Description |
---|---|---|---|---|---|
ServiceNames | True | String[] | A string array of service names to restart. |
Output
Location of output for log, result, and error files:
[System.Object]
.\Invoke-RestartService-log.txt