Reputation: 2078
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 ?
Upvotes: 0
Views: 1648
Reputation: 231
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);
}
}
Upvotes: 2