Willy,
I don't mean to hijack this thread, but I have an issue that revolves
around
this topic that I'm hoping you'll know the anser to. Basically, I can
access
root\cimv2 classes from ASP.NET (and VBScript), but using the same
methods, I
can't access root\MicrosoftIISv2. I get an Access Denied just trying to
do a
.Get or .CreateInstance.
Any ideas?
Thanks,
Anton
:
Willy,
The application is IIS and the file that I am wanting to Stop and Start
is
inetinfo.exe. Right now, I am using the System.Diagnostics.Process
class
to
get a list of running processes and find the inetinfo.exe process to
kill
it.
Unfortunately, I don't see a way to start a process remotely and I am
concerned about what security access rights are required to start and
stop
processes on a remote machine.
Thanks,
Yosh
IIS runs as a service, so you have to issue a Start/stop command through
the
Service Control Manager (SCM).
There are several ways to do this:
1. The easiest is to use the sc.exe commandline utility, but here you
need
to run as a local administrator on IIS the server, or a Domain admin .
2. Using System.Management namespace classes and the WMI IISWebService
class
http://msdn.microsoft.com/library/d...html/af1a277b-e67a-41b3-9947-91c9304f8ec7.asp.
Note that this requires IIS6 on the server (w2k3)
3. Using System.Management namespace classes and the WMI Win32_Service
class. Can be used for all IIS 5 and IIS6.
Here is a sample for option 2, option 3 is quite similar, consult MSDN
for
details about WMI.
using System;
using System.Management;
using System.Diagnostics;
public class Wmis {
public static void Main() {
ConnectionOptions co = new ConnectionOptions();
//get user and password
co.Username = "domain\\administrator"; // here domain can be the IIS
servername or a domain name
co.Password = "hispwd";
co.Authentication = AuthenticationLevel.PacketPrivacy; // This is the
minimum authentication level allowed
ManagementScope ms = new
ManagementScope(@"\\YourIISServer\root\MicrosoftIISv2", co);
ServiceAction(ms, "StopService"); // Stop IIS
ServiceAction(ms, "StartService"); // Start IIS
}
static void ServiceAction( ManagementScope ms, string ServiceAction)
{
string mp = String.Format("IIsWebService.Name='W3SVC'");
using(ManagementObject oW3SVC = new ManagementObject(ms, new
ManagementPath(mp), null))
{
ManagementBaseObject outParams = oW3SVC.InvokeMethod(ServiceAction,
null,
null);
// Handle the return code, here simply display the return value
Console.WriteLine
((System.UInt32)(outParams.Properties["ReturnValue"].Value));
}
}
}
Willy.