2

I want to perform iisreset programmatically from C# code over a list of servers with account having privilege to do that.

It's easy to do that for local machine for example that's a sample code:

    // using ...
using System.Diagnostics;

public class YourForm : Form
{
   // ...

   private void yourButton_Click(object sender, EventArgs e)
   {
      Process.Start(@"C:\WINDOWS\system32\iisreset.exe", "/noforce");
   }

   // ...
}

Also:

    using System.ServiceProcess;

using (ServiceController controller = new ServiceController())
{
controller.MachineName = “My local or remote computer name”;
controller.ServiceName = “IIS Service Name”; // i.e “w3svc”

if (controller.Status != ServiceControllerStatus.Running)
{
// Start the service
controller.Start();

Log.Debug(“IIS has been started successfully, now checking again for webservice availability”);

}
else
{
// Stop the service
controller.Stop();

// Start the service
controller.Start();

Log.Debug(“IIS has been restarted successfully”);

}

}

but how to perform this for more than one server.

Marzouk
  • 2,650
  • 3
  • 25
  • 56
  • What do you exactly mean by more than 1 server? Do you have a list of servers? If yes then can't you loop through them and execute the above code for each of them? – samar Nov 19 '15 at 11:17
  • I have a list of servers that the account execute the code have access on them as admin, i need from any of this servers to execute iisreset for all of them, i know that the above code working only for local machines. – Marzouk Nov 19 '15 at 11:19

2 Answers2

2

Your first code snippet should work perfectly taking in considerations that there is no need to provide the full path of iisreset command.

Actually, you don't need that full path while calling IISRESET from CMD or Run tool. So, it is the same call.

Regarding user privilege, there are 2 approaches

  1. You can pass desired user as an argument to Process.Start

    Process.Start("iisreset", "server1", "admin", "admin password", "domain");

  2. You can just call Process.Start as you did in your code, then make sure to run your application with the suitable user

I tried below and it worked perfectly

    static void Main(string[] args)
    {            
        string[] servers = LoadServersFromFile(); 

        foreach (string server in servers)
        {
            Process.Start("iisreset", server.Trim());
        }            
    }

    private static string[] LoadServersFromFile()
    {
        //just listed all servers comma separated in a text file, change this to any other approach fits for your case
        TextReader reader = new StreamReader("Servers.txt");
        return reader.ReadToEnd().Split(',');
    }
Muhammad Gouda
  • 849
  • 8
  • 20
1

You probably need an impersonator to execute the above code.

I think the username and password used in the impersonator should have admin rights for that server (which you do).

You probably also need to remotely access the machine and then execute your code.

The post here, here and here might be of help to you.

Will update this post if something more useful comes to my mind.

EDIT:

You can try out the following steps:

  1. Create a windows service with code for restarting the IIS
  2. Deploy this service on all the servers for which you need to reset the IIS
  3. Keep this service turned off
  4. Remotely access this service (code to access services remotely is given in one of the posts above)
  5. Start and stop the service. This will execute the code for resetting the IIS. Code for this is given here

Hope this helps.

Community
  • 1
  • 1
samar
  • 5,021
  • 9
  • 47
  • 71
  • Why you need to remote the machine? to reset iis on another machine, you just pass machine name or IP as an argument to iisreset command – Muhammad Gouda Nov 19 '15 at 12:47