I need to stop IIS on a remote machine and then do some work and then start the IIS service again once the work is done. I am trying to do this using C# code. I have seen some similar questions about starting IIS on remote machines through code. But I have not been able to get any working solution from it. Some clearcut C# code about how to do the start and stop operations would be really helpful.
-
Did you try anything? – Soner Gönül Jul 24 '13 at 10:33
-
Yes there was this solution about starting a command prompt as a Process doing IISReset using this command : IISRESET \\RemoteServerMachineName /stop – Quest Jul 24 '13 at 10:46
-
Are you trying to write something in C#? Or do you just want a command line tool? IISRESET will reset an IIS service on the same server. You can also do iisreset [COMPUTERNAME] /restart to restart from another computer but this may require firewall changes. I'm unclear though if you're looking for a c# solution which I gave you below or a command line / powershell script. – Mark Jul 24 '13 at 10:56
-
Hi Mark C# code would be better. But the code in your answer is for starting the service in the local machine right? I need to do this on a remote machine. Any suggestions on that? – Quest Jul 24 '13 at 11:11
2 Answers
The Microsoft.Web.Administration
Namespace has what you need for administering IIS. Check it out at: http://msdn.microsoft.com/en-us/library/microsoft.web.administration%28VS.90%29.aspx.
However if you just want to stop and start services you can manage your services using the Service.Controller
ServiceController service = new ServiceController(serviceName);
TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds);
service.Start();
service.WaitForStatus(ServiceControllerStatus.Running, timeout);
The Service.Controller
class page should have all the information you require. Find it at, http://msdn.microsoft.com/en-us/library/system.serviceprocess.servicecontroller.aspx
Here's a complete example you can use to start a service:
public static void StartService(string serviceName, int timeoutMilliseconds)
{
ServiceController service = new ServiceController(serviceName);
try
{
TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds);
service.Start();
service.WaitForStatus(ServiceControllerStatus.Running, timeout);
}
catch
{
// ...
}
}
Review the link above for more documentation.
You can connect to a remote server as follows:
ServiceController svc = new ServiceController("Service", "COMPUTERNAME");
If you require a different set of permissions on the remote server there's a lot more work involved. See this question, Starting remote Windows services with ServiceController and impersonation.
-
Hi Mark C# code would be better. But the above code is for starting the service in the local machine right? I need to do this on a remote machine. Any suggestions on that? – Quest Jul 24 '13 at 11:04
-
I just edited my example with the code required to call ServiceController on another server. Of course you'll have to ensure the account you're using has the required permissions on the destination server. – Mark Jul 24 '13 at 11:12
-
I think I don't have the permission to do this. I am getting "Cannot open iisadmin service on computer" exception. What if I do have the credentials of the user account that has access to start and stop the IIS service. How to specify those credentials in the code? – Quest Jul 24 '13 at 11:36
-
You have any Idea about how to specify user account credentials in the code for this Mark? – Quest Jul 24 '13 at 11:44
-
With permissions things gets a lot more complicated. See this answer for more information http://stackoverflow.com/questions/8581931/starting-remote-windows-services-with-servicecontroller-and-impersonation – Mark Jul 24 '13 at 11:45
-
What is the `Service` or `ServiceName` in the above context. Can you please give an example. – venkat Jan 20 '16 at 12:19
Solution A:
You can create a .bat file runtime like:
# for stopping
iisreset -stop
# for starting
iisreset -start
Then execute it on the remote machine using PsExec.
Solution B: Start or stop the "IISADMIN" Service directly. To manage remote services you can use below links:
- Monitor and Manage Services on Remote Machines
- Using the ServiceController in C# to stop and start a service
Be lucky ;)

- 451
- 5
- 16