Programmer to ProgrammerTM  
Wrox Press Ltd  
   
  Search ASPToday Living Book ASPToday Living Book
Index Full Text
 
ASPToday Home
 
 
Home HOME
Site Map SITE MAP
Index INDEX
Full-text search SEARCH
Forum FORUM
Feedback FEEDBACK
Advertise with us ADVERTISE
Subscribe SUBSCRIBE
Bullet LOG OFF
                         
      The ASPToday Article
December 7, 2000
      Previous article -
December 6, 2000
  Next article -
December 8, 2000
 
   
   
   
Remote Management of Windows System Services via ASP.NET   Srinivasa Sivakumar  
by Srinivasa Sivakumar
 
CATEGORIES:  .NET Framework, Security/Admin  
ARTICLE TYPE: Cutting Edge Reader Comments
   
    ABSTRACT  
 
Article Rating
 
   Useful
  
   Innovative
  
   Informative
  
 29 responses

Remote Administration is always a hot topic amongst system administrators and corporations. If we are not in close contact with our servers, it can be extremely inconvenient to attempt to administer any changes like stopping and starting the system services. In this article, Srinivasa Sivakumar shows us how to use the .NET SDK to remotely access Windows System Services.

   
                   
    Article Discussion   Rate this article   Related Links   Index Entries  
   
 
    ARTICLE

Remote Management of Windows System Services via ASP.NET 

Remote Administration is always a hot topic amongst system administrators and corporations. There are plenty of tools available for remote administration and they vary in price from a few hundreds of dollars to a few thousands of dollars. A few weeks ago, I wrote an article about WMI (Windows Management Instruments) which shows us how to access the Windows System Services. The .NET SDK has a namespace that allows us to access system services. In this article, we'll see the ways that we can access the system services via .NET, ASP.NET and VB.NET.

Why do we need to access System Services?

System services are the building blocks of the Windows operating system. The operating system itself depends on a few services and they can't be stopped or paused. However, occasionally the system services can fail to start, or we may wish to start, stop, or pause many of the other services available. If the service fails then as the user or the administrator we've to start the service manually via the control panel applet.

When we are administrating multiple servers, the option of remote administration of the services panel would help a great deal. Even when we are developing an ASP.NET application, we may want to stop and start the "IIS Admin Service". If our sever is on a different floor (or building!) then we've to walk to the different floor to reach the server.

Maybe we are writing an ASP.NET page to send e–mails and your mails are not delivered. We therefore want to check the status of the " Microsoft SMTP Service ". If the service is stopped we will want to start it. If we've an ASP.NET page which can do this for us then we are saving a lot of our time.

Note: In ASP.NET we've an option to store the session state in an external process called "Session State Store" which is managed by the system service process "State Server Process" – see Steve Schofield's ASPToday article.

Things to remember

Stopping and starting the system services can cause strange effects on the server. Some of the system services can depend on other running services. Stopping a service could cause problems to other running dependent services. Therefore, be careful when you are playing with the system services on the production server.

When we are doing remote administration, we'll be logging in to the server as an administrator. Now our server is open for any changes. Anyone can log on to our server and make it crash – so consider appropriate security measures such as SSL before setting up a remote admin site. See Byron Hynes ASPToday article for more information on SSL.

Accessing Services via .NET Namespace

We can access the system service via the System.ServiceProcess namespace. The ServiceController class exposes methods and properties to access the system services. First let's write a small ASP.NET page to list all the system services:

<% @Assembly Name="System.ServiceProcess" %>
<% @Import Namespace="System.ServiceProcess" %>
<html>
<head><title>View Services Via ASP.NET</title></head>
<body>
<script language="vb" runat="server">
Sub Page_Load (sender as Object, e as EventArgs) 

   Dim objServices() as ServiceController = ServiceController.GetServices()
   Dim obj as object

   Response.write("<H2>Win32 Services:</H2><HR><UL>")

   For each obj in objServices
      Response.write("<LI>DisplayName: " & obj.Displayname & _
      "<BR>Service Name: " & obj.ServiceName & _
      "<BR>Can be Stopped: " & obj.CanStop & _
      "<BR>Can be Paused: " & obj.CanPauseAndContinue & _
      "<BR>Can be Shutdown: " & obj.CanShutdown)
   Next
   Response.write ("</UL>")

   objServices = Nothing
   obj = Nothing
End Sub
</script>
</body>
</html>

In the above page, we're importing the System.ServiceProcess assembly and namespace. Then in the Page_Load event we're declaring a new object array of the type ServiceController and then initializing the variable with the system services collection by calling the GetServices method of the ServiceController class. The GetServices method returns a collection of ServiceController objects. Then we're looping through the collection and listing the system services information. Here is how the output looks:

We can also bind the system services collection into a data bound ASP.NET server control such as a DataGrid control. Here is the code to do it:

<% @Assembly Name="System.ServiceProcess" %>
<% @Import Namespace="System.ServiceProcess" %>
<html>
<head><title>View Services Via ASP.NET</title></head>
<body>
<script language="vb" runat="server">
Sub Page_Load (sender as Object, e as EventArgs) 

   Dim objServices as object
   objServices = ServiceController.GetServices
     
   ServiceBoundGrid.DataSource = objServices
   ServiceBoundGrid.DataBind
End Sub
</script>
<H2>Win32 Services:</H2><HR>

<form runat="server">
<asp:DataGrid id="ServiceBoundGrid" runat="server"
    BorderColor="black"
    BorderWidth="1"
    GridLines="Both"
    CellPadding="3"
    CellSpacing="0"
    Font-Name="Arial"
    Font-Size="8pt"
    HeaderStyle-BackColor="Silver"  
    AutoGenerateColumns="false">
    <property name="Columns">
      <asp:BoundColumn HeaderText="Service name" DataField="ServiceName" />
      <asp:BoundColumn HeaderText="Display name" DataField="DisplayName" />
      <asp:BoundColumn HeaderText="Status" DataField="Status"/>
      <asp:BoundColumn HeaderText="Can be stopped" DataField="CanStop" />
      <asp:BoundColumn HeaderText="Can be Paused" DataField="CanPauseAndContinue" />
      <asp:BoundColumn HeaderText="Can be Shutdowned" DataField="CanShutdown"/>
    </property>
</asp:DataGrid>
</form>
</body>
</html>

In the above code, as usual we're importing the System.ServiceProcess assembly and namespace. Then we're declaring a variable of the type object and we're assigning the services collection into the object variable. Then we're binding the object variable to the DataGrid server control. Here is how the output looks:

Let's improve the above code with few enhancements. We're going to add the status of the service to the output. Based on the status we'll display the associated action to the service. For example, if the service is currently running then, we'll display options to pause and stop the service. If the service is currently stopped then we'll display option to start the service. Here is the code which does it:

Response.write("<H2>Win32 Services:</H2><HR><UL>")
For each obj in objServices
Response.write("<LI>DisplayName: " & obj.Displayname & _
   "<BR>Service Name: " & obj.ServiceName & _
   "<BR>Can be stopped: " & obj.CanStop & _
   "<BR>Can be Paused: " & obj.CanPauseAndContinue & _
   "<BR>Can be Shutdowned: " & obj.CanShutdown & _
   "<BR>Status: ")

   'List the current status of the service
   Select Case obj.Status
      Case ServiceControllerStatus.ContinuePending
         Response.Write("continue pending" & "</LI>")
      Case ServiceControllerStatus.Paused:
         Response.Write("paused | <a href=""Cont.Aspx?SvrcName=" & _
obj.ServiceName & """>Continue Service</a></LI>")
      Case ServiceControllerStatus.PausePending:
         Response.Write("pause pending" & "</LI>")
      Case ServiceControllerStatus.Running:
         Response.Write("running | <a href=""Stop.Aspx?SvrcName=" & _
obj.ServiceName & """>Stop Service</a> | " & _
<a href=""Pause.Aspx?SvrcName=" & _
obj.ServiceName & """>Pause Service</a></LI>")
      Case ServiceControllerStatus.StartPending:
         Response.Write("start pending" & "</LI>")
      Case ServiceControllerStatus.Stopped:
         Response.Write("stopped | <a href=""Start.Aspx?SvrcName=" & _
obj.ServiceName & """>Start Service</a></LI>")
      Case ServiceControllerStatus.StopPending:
         Response.Write("stop pending" & "</LI>")
   End Select
Next
Response.write ("</UL>")

In the above code, we're checking the status of the service and we're displaying options to act. And here is how the output looks:

Starting and Stopping services

The ServiceController class has methods to start and stop the services. In the following code, we're going to start the service, with the service name being passed by the Services.Aspx page:

<% @Assembly Name="System.ServiceProcess" %>
<% @Import Namespace="System.ServiceProcess" %>
<script language="vb" runat="server">
Sub Page_Load (sender as Object, e as EventArgs) 

   Dim strSvrcName as String = request.Querystring("SvrcName")
   Dim objService as ServiceController =new ServiceController(strSvrcName)

   objService.Start

   objService = Nothing

   server.transfer("Services.Aspx")
End Sub
</Script>
</body>
</html>

As usual, we're importing the assembly and namespace. Then we're declaring a string variable and initialing the variable with the service name supplied via the QueryString collection (We can now initialize our variables in VB.NET). Then we're declaring another variable of the type ServiceController and we're initialing the variable with the service name. To initialize, we're accessing the constructor of the ServiceController object and passing the service name as the parameter to it. Then we're using the start method to start the service. Let's stop a service with the following code:

Sub Page_Load (sender as Object, e as EventArgs) 

   Dim strSvrcName as String = request.Querystring("SvrcName")
   Dim objService as ServiceController = new ServiceController(strSvrcName)

   If objService.CanStop = True then
      objService.Stop
      objService = Nothing
      server.transfer("Services.Aspx")
   Else
      Response.Write("This service can't be stopped")
   End if

   objService = Nothing
End Sub

This code is identical to the previous start services code. The only difference is that before stopping the service with the stop method, we're checking to see that if we can stop this service via the CanStop method. This is because some Windows Services can't be stopped due to internal reasons.

Pausing and continuing services

We can pause a running service using the following code:

Sub Page_Load (sender as Object, e as EventArgs) 

   Dim strSvrcName as String = request.Querystring("SvrcName")
   Dim objService as ServiceController = new ServiceController(strSvrcName)

   If objService.CanPauseAndContinue = True then
      objService.Pause
      objService = Nothing
      server.transfer("Services.Aspx")
   Else
      Response.Write("This service can't be Paused")
   End if

   objService = Nothing
End Sub

This code is identical to the previous stop services code. The only difference is that we are checking to see if this service can be paused. If so then we're pausing the service; otherwise, we're displaying a message.

We can continue a paused service with following code:

Sub Page_Load (sender as Object, e as EventArgs) 

   Dim strSvrcName as String = request.Querystring("SvrcName")
   Dim objService as ServiceController = new ServiceController(strSvrcName)

   objService.Continue 

   objService = Nothing
   server.transfer("Services.Aspx")
End Sub

Installing the sample files

To run the sample attached with this article you'll need the Preview or Beta1 version of the .NET Framework SDK, downloadable from Microsoft's website at the link provided below. Important Note: Before installing the .NET SDK Beta1, make sure you uninstall the Preview version of .NET SDK if you are using it. Then create a virtual directory, place the files from the download in the directory, and surf to Services.aspx .

Summary

In this article we've seen the ways to list, start, stop, pause and continue Windows System Services. We've also seen the way to bind the services to the server control. Whatever code we've used here is native to .NET. Thus it should give us all the advantages of .NET over external COM components for accessing the same information.

 
 
   
  RATE THIS ARTICLE
  Please rate this article (1-5). Was this article...
 
 
Useful? No Yes, Very
 
Innovative? No Yes, Very
 
Informative? No Yes, Very
 
Brief Reader Comments?
Your Name:
(Optional)
 
  USEFUL LINKS
  Related Tasks:
 
 
   
  Related ASPToday Articles
   
  • Session State in ASP.NET (November 22, 2000)
  • An Introduction to Windows Management Instrumentation (WMI) with ASP (September 27, 2000)
  • SSL: Both Secure and Friendly - Part I/II: Why and How to use SSL. (December 6, 1999)
  •  
           
     
     
      Related Sources
     
  • Download the .NET Framework SDK beta 1: http://msdn.microsoft.com/downloads/default.asp?URL=/code/sample.asp?url=/msdn-files/027/000/976/msdncompositedoc.xml
  • VS.NET Subscriber Download: http://msdn.microsoft.com/subscriptions/resources/subdwnld.asp
  • VS.NET FAQ: http://msdn.microsoft.com/vstudio/nextgen/qa.asp
  • ASPNextGen: http://www.aspnextgen.com/
  • Dotnetbooks: http://www.dotnetbooks.com/
  • Dotnettoday: http://www.dotnettoday.com/
  • Dotnetwire: http://www.dotnetwire.com/
  •  
     
           
      Search the ASPToday Living Book   ASPToday Living Book
     
      Index Full Text Advanced 
     
     
           
      Index Entries in this Article
     
  • .NET Framework
  •  
  • accessing
  •  
  • ASP.NET
  •  
  • CanPauseAndContinue property
  •  
  • CanStop property
  •  
  • Continue method
  •  
  • DataGrid
  •  
  • GetServices method
  •  
  • namespaces
  •  
  • Pause method
  •  
  • pausing and continuing
  •  
  • QueryString collection
  •  
  • remote administration
  •  
  • Request object
  •  
  • ServiceController class
  •  
  • START method
  •  
  • starting
  •  
  • Stop method
  •  
  • stopping
  •  
  • system services
  •  
  • system services, accessing
  •  
  • VB.NET
  •  
  • Visual Basic
  •  
     
     
    HOME | SITE MAP | INDEX | SEARCH | REFERENCE | FEEDBACK | ADVERTISE | SUBSCRIBE
    .NET Framework Components Data Access DNA 2000 E-commerce Performance
    Security Admin Site Design Scripting XML/Data Transfer Other Technologies

     
    ASPToday is brought to you by Wrox Press (http://www.wrox.com/). Please see our terms and conditions and privacy policy.
    ASPToday is optimised for Microsoft Internet Explorer 5 browsers.
    Please report any website problems to webmaster@asptoday.com. Copyright © 2001 Wrox Press. All Rights Reserved.