0

I have the IIS service running on Windows Server 2008 and i want to reset it from my windows 7 machine .

How do i reset IIS remotely using C#.Net code?

I tried using Microsoft.Web.Administration, but it doesn't accept remote server details to connect to .Any other API's that can be used for this purpose ?

1

1 Answer 1

2

If you have a specific web site name, you can use the WMI to stop and start that web site. If it's IIS as a whole, I'm sure there is something in the IIS WMI provider for doing this. Here I'm using WebAdministration to manage a web site under IIS, but there's also MicrosoftIISV2. Google for WmiExplorer, there are some good ones out there.

        var connOptions = new ConnectionOptions();
        connOptions.Authentication = AuthenticationLevel.PacketPrivacy;
        // if you want to connect as someone other than logged in user
        //connOptions.Username = username;
        //connOptions.Password = password;

        var scope = new ManagementScope("\\localhost\WebAdministration", connOptions);

        WqlObjectQuery query = new WqlObjectQuery(`enter code here`string.Format("SELECT * FROM Site WHERE Name = '{0}'", "Default Web Site"));

        using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query))
        {
            foreach (ManagementObject site in searcher.Get())
            {
                ManagementBaseObject inParams = site.GetMethodParameters("Stop");
                site.InvokeMethod("Stop", inParams, null);

                ManagementBaseObject inParams2 = site.GetMethodParameters("Start");
                site.InvokeMethod("Start", inParams2, null);
            }
        }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.