0

I am creating a small one page asp.net web forms application to reset IIS. This page contains a gridview with checkbox column to select the machines whose IIS reset is to be done. Then based on selection I will have to do the IIS reset of all selected machines.

Now the web application is hosted on Machine 1 on IIS and from this machine I will run the web application and select may be Machine 2, Machine 3 for IIS reset. I have written some code for IIS reset but it is giving me exception of Access Denied. Details are below.

try
{
    using (ServiceController iisService = new ServiceController("W3SVC"))
    {
        TimeSpan timeout = TimeSpan.FromMilliseconds(10000);
        iisService.MachineName = machineName;

        if (iisService.Status == ServiceControllerStatus.Running)
        {
            iisService.Stop();
            iisService.WaitForStatus(ServiceControllerStatus.Stopped, timeout);
        }

        if (iisService.Status == ServiceControllerStatus.Stopped)
        {
            iisService.Start();
            iisService.WaitForStatus(ServiceControllerStatus.Running, timeout);
        }
    }
}

After running the above code, I am receiving the below error:

cannot open w3svc service on computer 'machine name'

Also is there any other way I can do this, but I have to provide option for machine selection like a grid or something.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
keyur sheth
  • 29
  • 1
  • 2
  • What is the purpose of the `try/catch` block? Also, do you have any reason to believe that you have access to IIS on that machine? Have you tried using IIS Manager remotely? – John Saunders Jun 23 '15 at 04:35
  • What kind of authentication are you using? Windows authentication? – John Saunders Jun 23 '15 at 04:36
  • The user you're running your application under should have the proper permissions to do the operation you're trying to do. – Moayad Mardini Jun 23 '15 at 04:39
  • You could use powershell to [achieve this](http://stackoverflow.com/questions/1356856/how-to-start-stop-iis-6-0-7-0-remotely-using-powershell-scripts). Is this a once off event or scheduled ? – lloyd Jun 23 '15 at 04:42
  • Try catch is for exception, I get InvalidOperationException. I am logging to Machine 1 with administrator account and the same account is Administartor on all other machines. No I have not used IIS Manager remotely, sorry idea about that. Yes I have windows authentication enabled but I do not ask for any credentials. In case of power shell script after creating it do I have to call it from my asp.net application ? – keyur sheth Jun 23 '15 at 04:58
  • Search term: "NTML one hop authentication". You need Kerberos authentication to get it working. – Alexei Levenkov Jun 23 '15 at 05:18

0 Answers0