Search This Blog

Friday, February 18, 2011

Restart Remote Machine IIS from Code

If anybody have requirement to restart Remote Machine IIS through code, then this is quite simple.

Scenario: If you are developing some assembly/dll, and need to deploy that dll in GAC. Each time you deploy that assembly in GAC, you must re-start IIS to see the changes.

Following code will make your life easy, because without going to server you can re-start IIS of that server.

Note:-To re-start remote machine IIS you must be an administrator of the remote computer. Either have your account added to the administrator local group of the remote computer or to the domain administrator global group.

using System.Diagnostics;
using System;

namespace ResetIISConsole
{
class Program
{
static void Main(string[] args)
{
string serverName = string.Empty;
try
{
//Read input from Console
//First argument as ServerName
serverName = args[0];
//IISReset Process resides in WINDOWS\SYSTEM32 Folder
Process iisreset = Process.Start("iisreset.exe", @"C:\windows\system32");
//Pass Name of the server
iisreset.StartInfo.Arguments = serverName;
iisreset.Start();
Console.WriteLine("SUCCESS: Done!!!");
}
catch (Exception ex)
{
Console.WriteLine("ERROR:" + ex.Message);
}
}
}
}


Another way to do this is simple command:

c:\Windows\System32>IISRESET [COMPUTERNAME]


No comments:

Post a Comment