Create a Web Service Method to Get NT Service Information

Recently, I created a SQL Server Management tool for mobile devices, Siccolo, that allows me to manage SQL Servers by using web services hosted on public domain. (See more information on the Siccolo web site about how to develop a mobile management tool: developing Siccolo—a mobile management tool.)

As a part of the management tool, I needed to show some information about selected NT Service, such as the path to the service executable. For example, services.msc shows it like this:

In my mobile management tool, I needed to display in in a similar manner:

The code presented retrieves information about Path to Executable for selected NT Service.

Background (Optional, but Needed)

My "managing" web service is hosted under SSL with "Integrated Windows" authentication set. Therefore, a mobile application is required to pass network credentials. This is needed for remote access to get information from the Registry on the remote machine.

Using the Code

Components used:

  • serviceprocessor.asmx.cs: Web service interface
  • NTServiceInfo.cs: A small "wrapper" to retrieve NT Service information

.NET provides just the tool for this task, the System.ServiceProcess.ServiceController class. To create an instance of System.ServiceProcess.ServiceController, do the following:

// C# //
...
System.ServiceProcess.ServiceController Service;
if (this.m_MachineName!="")
   {Service = new ServiceController(this.m_ServiceName,
                                    this.m_MachineName ) ;}
else
{Service = new ServiceController(this.m_ServiceName ) ;}
...

The fact that authentication (Integrated Windows authentication or Basic) is in place on IIS actually helps here. To be able to access service(s) on the different machine than the web service host, the web service needs to "assume" the identity of an authenticated user. Normally, the web service is running under an ASP.NET user with minimum privileges; I needed to impersonate an authenticated user with the web service.

On the server side, to retrieve authenticated user, you need to use System.Web.Services.WebService.User and then impersonate: The code does just that—"Impersonates the user represented by the WindowsIdentity object."

// C# //
...

System.Security.Principal.WindowsImpersonationContext
   impersonationContext;
   impersonationContext = 
      ((System.Security.Principal.WindowsIdentity)User.Identity).
      Impersonate();
...

To retrieve the path to the executable, you can look under HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services, find the selected service, and get "ImagePath":

To read the value of a Registry key (on the remote machine, or on the local machine):

private string ReadRegestryKey(string RegistryKey,
                               out string ErrorInfo)
{
   try
   {
      string Value="";
      ErrorInfo ="";

      RegistryKey Key;
      RegistryKey KeyHKLM = Registry.LocalMachine;
      try
      {
         if (this.m_MachineName !="" )    //open on remote machine
            Key = Microsoft.Win32.RegistryKey.OpenRemoteBaseKey(
               RegistryHive.LocalMachine, this.m_MachineName
                  ).OpenSubKey(RegistryKey);
         else
            Key = KeyHKLM.OpenSubKey(RegistryKey);

         Value = Key.GetValue("ImagePath").ToString();
         Key.Close();
      }
      catch (Exception ex_open_key)
      {
         ErrorInfo = "Error Accessing Registry
            [" + ex_open_key.ToString() + "]";
         return "";
      }
      return Value;
   }
   catch (Exception ex_read_registry)
   {
      ErrorInfo =   ex_read_registry.Message;
      return "";
   }
}

Create a Web Service Method to Get NT Service Information

Once Path to executable is extracted, you need to check whether the path needs to be "extracted" from something like "%SystemRoot%\system32\..." to the actual path.

The value of %SystemRoot% can be found in the Registry:

private string ExpandEnvironmentString (string Path)
{
   string SystemRootKey =
      "Software\\Microsoft\\Windows NT\\CurrentVersion\\";
   RegistryKey Key;

   if (this.m_MachineName !="" )
      Key = Microsoft.Win32.RegistryKey.OpenRemoteBaseKey(
         RegistryHive.LocalMachine, this.m_MachineName
            ).OpenSubKey(SystemRootKey);
   else
      Key = Registry.LocalMachine.OpenSubKey(SystemRootKey);

   string ExpandedSystemRoot ="";
   ExpandedSystemRoot = Key.GetValue("SystemRoot").ToString();
   Key.Close();

   Path = Path.Replace ("%SystemRoot%", ExpandedSystemRoot);

   return Path;
}

And, finally:

public string PathToExecutable(WindowsPrincipal User)
{
   //HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services.
   string RegistryKey = "SYSTEM\\CurrentControlSet\\Services\\" +
      this.m_ServiceName;

   string ErrorInfo="";
   System.Security.Principal.WindowsImpersonationContext
      impersonationContext;
   impersonationContext =
      ((System.Security.Principal.WindowsIdentity)User.Identity).
      Impersonate();

   string Path= this.ReadRegestryKey(RegistryKey, out ErrorInfo);
   if ( Path.IndexOf("%")>0)
   {
      Path = ExpandEnvironmentString(Path);
   }
   impersonationContext.Undo();
   return Path;
}

Notice how to call to ReadRegestryKey() and call ExpandEnvironmentString() "wrapped in":

impersonationContext =
   ((System.Security.Principal.WindowsIdentity)User.Identity).
      Impersonate();
...
...
impersonationContext.Undo();

Then, the actual web method:

[WebMethod]
public bool GetNTServiceInfo(string RemoteServerAddress ,
                             string NTServiceName,
                             out string ServiceInfo_XML ,
                             out string ErrorInfo )
{
   try
   {
      string ToDebugSetting =
         System.Configuration.ConfigurationSettings.AppSettings.
         Get("DebugMode");
      bool ToDebug = (ToDebugSetting!="");

      ErrorInfo="";
      ServiceInfo_XML ="";
      System.ServiceProcess.ServiceController Service;
      if (RemoteServerAddress!="")
      {Service = new ServiceController(NTServiceName,
                                       RemoteServerAddress ) ;}
      else
      {Service = new ServiceController(NTServiceName ) ;}

      DataSet objDataSet = new DataSet("QueryResults");
      objDataSet.Tables.Add("ServiceInfo");
      objDataSet.Tables[0].Columns.Add("service_display_name",
         System.Type.GetType("System.String"));
      objDataSet.Tables[0].Columns.Add("status",
         System.Type.GetType("System.String"));
      objDataSet.Tables[0].Columns.Add("service_name",
         System.Type.GetType("System.String"));
      objDataSet.Tables[0].Columns.Add("path_to_executable",
         System.Type.GetType("System.String"));
      objDataSet.Tables[0].Columns.Add("can_stop",
         System.Type.GetType("System.Boolean"));
      objDataSet.Tables[0].Columns.Add("can_pause_and_continue",
         System.Type.GetType("System.Boolean"));
      objDataSet.Tables[0].Columns.Add("services_depend_on",
         System.Type.GetType("System.String"));
      objDataSet.Tables[0].Columns.Add("dependent_services",
         System.Type.GetType("System.String"));

      NTServiceInfo si  = new NTServiceInfo(NTServiceName,
                                            RemoteServerAddress);

      Object[] r = new Object[8] {Service.DisplayName,
                                  Service.Status.ToString(),
                                  Service.ServiceName,
                                  si.PathToExecutable(
                                     (WindowsPrincipal)
                                     this.User),
                                  Service.CanStop.ToString(),
                                  Service.CanPauseAndContinue.
                                     ToString(),
                                  si.ServiceDependOnStringList(
                                     Service.ServicesDependedOn),
                                  si.DependentServicesStringList(
                                     Service.DependentServices)
                                 };

      objDataSet.Tables[0].Rows.Add(r);

      Service.Close();

      System.IO.StringWriter objStringWriter =
         new System.IO.StringWriter();
      objDataSet.WriteXml(objStringWriter, XmlWriteMode.WriteSchema);

      ServiceInfo_XML = "<?xml version='1.0' ?>" +
         objStringWriter.ToString();

      ErrorInfo = "";
      return true;
   }
   catch (Exception ex_get_service_info)
   {
      ServiceInfo_XML ="";
      ErrorInfo = ex_get_service_info.Message;
      return false;
   }
}

Points of Interest

If you want to read more on this story, please take a look at Siccolo—Free Mobile Management Tool For SQL Server and the full article at How to Develop Mobile Management Tool.



About the Author

Al Siks

check out the free SQL Server Management Tool for mobile devices at Siccolo

Comments

  • There are no comments yet. Be the first to comment!

  • You must have javascript enabled in order to post comments.

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Packaged application development teams frequently operate with limited testing environments due to time and labor constraints. By virtualizing the entire application stack, packaged application development teams can deliver business results faster, at higher quality, and with lower risk.

  • Live Event Date: September 16, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you starting an on-premise-to-cloud data migration project? Have you thought about how much space you might need for your online platform or how to handle data that might be related to users who no longer exist? If these questions or any other concerns have been plaguing you about your migration project, check out this eSeminar. Join our speakers Betsy Bilhorn, VP, Product Management at Scribe, Mike Virnig, PowerSucess Manager and Michele …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds