0

presently I am writing a small piece of code to get the list of web services hosted on IIS in a remote system,

the working code right now is :

       string q2 = "select * from Win32_PerfFormattedData_W3SVC_WebService";
        ManagementScope scope2 = new ManagementScope(string.Format(@"\\dtp-robaro2\{1}", host, ns), options); 

        // ns here is string ns = @"root\cimv2";

        scope2.Connect();

        ManagementObjectSearcher search2 = new ManagementObjectSearcher(scope, new ObjectQuery(q2));
        foreach (ManagementObject mo in search2.Get())
        {
            Console.WriteLine(mo.ClassPath);
            Console.WriteLine(mo.GetText(TextFormat.Mof));
        }

now I was wondering if WMI is turned off on the remote system that i am querying then is there any alternative way to access the information i get with the above code?

Aadi Droid
  • 1,689
  • 4
  • 22
  • 46
  • 1
    Your question doesn't make sense. Your WMI query is actually querying the [performance counter for the W3SVC WebService counter](http://msdn.microsoft.com/en-us/library/aa394298(v=vs.85).aspx?ppud=4). I am confused as to what you are trying to achieve. Do you want to list the installed system services on the operating system, or the websites that are running in IIS itself? In either case, using that WMI query will not likely achieve what you desire. – Tom Pickles May 31 '11 at 18:46
  • if you see the sentence i've written below the code then does it make sense? Anyway I am using SC now to get the list of services, its working fine, except I am not able to retrieve the list of Websites installed in IIS on the remote system :/ Any pointers? – Aadi Droid Jun 02 '11 at 05:02
  • Your question doesn't make sense because the code you've written will not do what you want, even if WMI was contactable on the remote system. SC.exe will only bring back installed services on the server, which is not what you want either. You're confusing services installed on the servers' OS and web services. They're completely different entities. – Tom Pickles Jun 02 '11 at 12:38

2 Answers2

1

Use tool Service Control - SC.EXE

lsalamon
  • 7,998
  • 6
  • 50
  • 63
  • sorry for being a noob, but how do i execute a query like I do on WMI to get a list of services running on IIS! – Aadi Droid May 31 '11 at 12:37
  • see example here [Windows command to get service status?](http://stackoverflow.com/questions/802121/windows-command-to-get-service-status/802188#802188) – lsalamon May 31 '11 at 13:14
  • 1
    Using the [ServiceController](http://msdn.microsoft.com/en-us/library/system.serviceprocess.servicecontroller.aspx) class is the proper way to do it in .Net, if you want to interact with the installed services on the OS. – Tom Pickles May 31 '11 at 18:51
  • It is possible to just retrieve the list of services right ? Anyway I'm working on it ! Will get back soon! – Aadi Droid Jun 01 '11 at 04:51
0

I have converted this from VB.Net to C# so it may not be exactly correct, but this will do what you need:

public List<string> GetSites(string MachineName)
{

    List<string> siteList = new List<string>();

    DirectoryEntry iis = new DirectoryEntry(string.Format("IIS://{0}/w3svc/1/root", MachineName));    

    foreach (DirectoryEntry site in iis.Children) {

        if (site.SchemaClassName == "IIsWebServer") {
            siteList.Add(site.Properties("ServerComment").Value.ToString());

        }
    }

    return siteList;

}
Tom Pickles
  • 890
  • 10
  • 26
  • your code was a good start, had to change the DirectoryEntry line, added w3svc/1/root and then listed the entries. The web services are there :) – Aadi Droid Jun 20 '11 at 08:23