- Restart-Service
- Syntax
- Description
- Examples
- Example 1: Restart a service on the local computer
- Example 2: Exclude a service
- Example 3: Start all stopped network services
- Parameters
- Inputs
- Outputs
- Notes
- Windows Services Recovery not restarting service
- 4 Answers 4
- Automatic services doesn’t start automatically after windows restart
- 10 Answers 10
- How can I restart a windows service programmatically in .NET
- 10 Answers 10
- An example using by ServiceController Class
Restart-Service
Stops and then starts one or more services.
Syntax
Description
The Restart-Service cmdlet sends a stop message and then a start message to the Windows Service Controller for a specified service. If a service was already stopped, it is started without notifying you of an error. You can specify the services by their service names or display names, or you can use the InputObject parameter to pass an object that represents each service that you want to restart.
Examples
Example 1: Restart a service on the local computer
This command restarts the Windows Management Instrumentation service (WinMgmt) on the local computer.
Example 2: Exclude a service
This command restarts the services that have a display name that starts with Net, except for the Net Logon service.
Example 3: Start all stopped network services
This command starts all of the stopped network services on the computer.
This command uses the Get-Service cmdlet to get objects that represent the services whose service name starts with net. The pipeline operator ( | ) sends the services object to the Where-Object cmdlet, which selects only the services that have a status of stopped. Another pipeline operator sends the selected services to Restart-Service .
In practice, you would use the WhatIf parameter to determine the effect of the command before you run it.
Parameters
Prompts you for confirmation before running the cmdlet.
Type: | SwitchParameter |
Aliases: | cf |
Position: | Named |
Default value: | False |
Accept pipeline input: | False |
Accept wildcard characters: | False |
Specifies the display names of services to restarted. Wildcard characters are permitted.
Type: | String [ ] |
Position: | Named |
Default value: | None |
Accept pipeline input: | False |
Accept wildcard characters: | True |
Specifies services that this cmdlet omits. The value of this parameter qualifies the Name parameter. Enter a name element or pattern, such as s*. Wildcard characters are permitted.
Type: | String [ ] |
Position: | Named |
Default value: | None |
Accept pipeline input: | False |
Accept wildcard characters: | True |
Forces the command to run without asking for user confirmation.
Type: | SwitchParameter |
Position: | Named |
Default value: | None |
Accept pipeline input: | False |
Accept wildcard characters: | False |
Specifies services that this cmdlet restarts. The value of this parameter qualifies the Name parameter. Enter a name element or pattern, such as s*. Wildcard characters are permitted.
Type: | String [ ] |
Position: | Named |
Default value: | None |
Accept pipeline input: | False |
Accept wildcard characters: | True |
Specifies ServiceController objects that represent the services to restart. Enter a variable that contains the objects, or type a command or expression that gets the objects.
Type: | ServiceController [ ] |
Position: | 0 |
Default value: | None |
Accept pipeline input: | True |
Accept wildcard characters: | False |
Specifies the service names of the services to restart.
Type: | String [ ] |
Aliases: | ServiceName |
Position: | 0 |
Default value: | None |
Accept pipeline input: | True |
Accept wildcard characters: | True |
Returns an object that represents the service. By default, this cmdlet does not generate any output.
Type: | SwitchParameter |
Position: | Named |
Default value: | None |
Accept pipeline input: | False |
Accept wildcard characters: | False |
Shows what would happen if the cmdlet runs. The cmdlet is not run.
Type: | SwitchParameter |
Aliases: | wi |
Position: | Named |
Default value: | False |
Accept pipeline input: | False |
Accept wildcard characters: | False |
Inputs
System.ServiceProcess.ServiceController, System.String
You can pipe a service object or a string that contains a service name to this cmdlet.
Outputs
None, System.ServiceProcess.ServiceController
This cmdlet generates a System.ServiceProcess.ServiceController object that represents the restarted service, if you specify the PassThru parameter. Otherwise, this cmdlet does not generate any output.
Notes
This cmdlet is only available on Windows platforms.
- Restart-Service can control services only when the current user has permission to do this. If a command does not work correctly, you might not have the required permissions.
- To find the service names and display names of the services on your system, type Get-Service «. The service names appear in the Name column, and the display names appear in the DisplayName column.
Windows Services Recovery not restarting service
I configure the recovery for Windows services to restart with a one minute delay after failures. But I have never gotten it to actually restart the service (even with the most blatant errors).
I do get a message in the EventViewer:
The description for Event ID ( 1 ) in Source ( MyApp.exe ) cannot be found. The local computer may not have the necessary registry information or message DLL files to display messages from a remote computer. You may be able to use the /AUXSOURCE= flag to retrieve this description; see Help and Support for details. The following information is part of the event: Access violation at address 00429874 in module ‘MyApp.exe’. Write of address 00456704.
Is there something else I have to do? Is there something in my code (I use Delphi) which needs to be set to enable this?
4 Answers 4
Service Recovery is intended to handle the case where a service crashes — so if you go to taskmgr and right click «end process» on your service process, the recovery logic should kick in. I don’t believe that the service recovery logic kicks in if your service exits gracefully (even if it exits with an error).
Also the eventvwr message indicates that your application called the ReportEvent API specifying event ID 1. But you haven’t registered your event messages with the event viewer so it can’t convert event ID 1 into a meaningful text string.
Service Recovery only works for unexpected exit like (exit(-1)) call. For all the way we use to stop the service in usual way will not works for recovery. If you want to stop service and still wants recovery to work, call exit(-1) and you will see error message as «service stopped with unexpected error» , and then your service will restart as recovery setting is.
The Service Control Manager will attempt to restart your service if you’ve set it up to be restarted by the SCM. This is detailed here in the documentation for the SERVICE_FAILURE_ACTIONS structure.
A service is considered failed when it terminates without reporting a status of SERVICE_STOPPED to the service controller.
This can be fine tuned by setting the SERVICE_FAILURE_ACTIONS_FLAG structure’s fFailureActionsOnNonCrashFailures flag, see here). You can set this setting from the Services applet by checking the «Enable actions for stops with errors» checkbox on the recovery tab.
If this member is TRUE and the service has configured failure actions, the failure actions are queued if the service process terminates without reporting a status of SERVICE_STOPPED or if it enters the SERVICE_STOPPED state but the dwWin32ExitCode member of the SERVICE_STATUS structure is not ERROR_SUCCESS (0). If this member is FALSE and the service has configured failure actions, the failure actions are queued only if the service terminates without reporting a status of SERVICE_STOPPED.
So, depending on how you have structured your service, how you have configured your failure actions AND what you do when you have your ‘fatal error’ it may be enough to call ExitProcess() or exit() and return a non zero value. However, it’s probably safest to ensure that your service exits without the code that’s dealing with the SCM telling the SCM that your service has reached the SERVICE_STOPPED state. This ensures that your failure actions ALWAYS happen.
Automatic services doesn’t start automatically after windows restart
I have windows 10 built 1511 with all the updates.
Today I have noticed some strange behavior :
when I make a system restart , after , some of the services with startup type Automatic doesn’t start automatically.
But I can start them manually , and after that everything is ok. But if I make a system restart , the problem appear again.
If I start the service manually , and after I do shutdown , everything is ok when I turn on again the PC. So the services are automatically started as should be. The problem appear only if I restart Windows. After restart , these services are not started automatically.
This problem appear only to some services that are related with applications ( not those that are system services ). These services doesn’t depend to any other service.
10 Answers 10
The only solution that works for the moment is to change the startup type of these services from Automatic to Automatic Delayed. I don’t know if this is the best solution , but is the only that works for now.
My previous answer did not fix our problem. In the end, we fixed it by setting the time out of the services to a higher value. Try this solution:
Go to Start > Run > and type regedit
Navigate to: HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control
With the control folder selected, right click in the pane on the right and select new DWORD Value
Name the new DWORD: ServicesPipeTimeout
Right-click ServicesPipeTimeout, and then click Modify
Click Decimal, type ‘180000’, and then click OK
Restart the computer
The Microsoft Windows Service Control Manager controls the state (i.e., started, stopped, paused, etc.) of all installed Windows services. By default, the Service Control Manager will wait 30,000 milliseconds (30 seconds) for a service to respond. However, certain configurations, technical restrictions, or performance issues may result in the service taking longer than 30 seconds to start and report ready to the Service Control Manager.
By editing or creating the ServicesPipeTimeout DWORD value, the Service Control Manager timeout period can be overridden, thereby giving the service more time to start up and report ready to the Service.
Reporting Server Error 1053: The service did not respond to the start or control request in a timely fashion
How can I restart a windows service programmatically in .NET
How can I restart a windows service programmatically in .NET?
Also, I need to do an operation when the service restart is completed.
10 Answers 10
Take a look at the ServiceController class.
To perform the operation that needs to be done when the service is restarted, I guess you should do that in the Service yourself (if it is your own service).
If you do not have access to the source of the service, then perhaps you can use the WaitForStatus method of the ServiceController .
This article uses the ServiceController class to write methods for Starting, Stopping, and Restarting Windows services; it might be worth taking a look at.
Snippet from the article (the «Restart Service» method):
An example using by ServiceController Class
You could also call the net command to do this. Example:
This answer is based on @Donut Answer (the most up-voted answer of this question), but with some modifications.
- Disposing of ServiceController class after each use, because it implements IDisposable interface.
- Reduce the parameters of the method: there is no need to the serviceName parameter being passed for each method, we can set it in the constructor, and each other method will use that service name.
This is also more OOP friendly. - Handle the catch exception in a way that this class could be used as a component.
- Remove the timeoutMilliseconds parameter from each method.
- Add two new methods StartOrRestart and StopServiceIfRunning , which could be considered as a wrapper for other basic methods, The purpose of those methods are only to avoid exceptions, as described in the comment.
Here is the class
Don’t forget to add the System.ServiceProcess.dll to your project for this to work.
Here is a snippet from the article.
I needed somethin more complex, because sometimes services with depencies couldnt be restarted and just throw exception or service could be set to «disabled» and so on.
So this is what i did:
(It checks if service does exist, if its «Disabled» it will set service to «Auto» and when it couldnt restart service it will use taskkill command to kill service through PID and then start it again (You need to be carefull with dependent services with this cause you will need to start/restart them too).
And it just returns true/false if restart was sucessfull
Tested on WIN10 only.
PS: working on version which detect dependent services when using taskkill and restart them too