Go4Expert

Go4Expert (http://www.go4expert.com/)
-   ASP (http://www.go4expert.com/articles/asp-tutorials/)
-   -   Check Windows System Services of Remote Machine (http://www.go4expert.com/articles/check-windows-services-remote-machine-t19281/)

naimish 3Sep2009 11:32

Check Windows System Services of Remote Machine
 

Introduction



This Solution will provide the status of the particular service and its exit code.
Usually to check the service the analyst need to login into remote machine with the Domain A/c and password which takes 10-15min.,where as this solution will fetch the information with in a min.Even we can extend this application to stop,start and restart the services.

Background



By Using System.Managent Name space and its classes ,I m passing the credentials that are required to access the system to the management scope class, and passing the Query which is written in WQL (Windows Query Language) to the object searcher class and fetching the results as collection and displaying the same. We can also make this code as DLL and can use it as resuable code

This Solution is web based solution and can be accessed anywhere in the workspace.

The code




Code:

Using System.Management;
public partial class CheckService : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
           
    }
    protected void CheckServices_Click(object sender, EventArgs e)
    {
       
        string check;
        Label1.Text = "";
        int i=0;
        if (Other_radio.Checked==true)
        {
            check = Service_txt.Text.Trim();  // Service_txt is text box  which gets  the Service name from the user if it is not in the list
        }
        else
        {
            check = ServiceList.SelectedValue; // Dropdown list which contains a list of serivce
        }
     
        try
        {
          ConnectionOptions oConn = new ConnectionOptions();
            oConn.Username = "Domain\\username";
            oConn.Password = "P@ssw0rd";
            System.Management.ManagementScope oMs =
                              new System.Management.ManagementScope("\\\\"+server_txt.Text+"\\root\\cimv2" , oConn);
            // Server_txt is text box  which gets  the Server name from the user
           
System.Management.ObjectQuery oQuery =
                new System.Management.ObjectQuery("select * from Win32_Service Where Name='" + check + "'");
                                                                                        //WQL to fetch the Data similar to SQL query
ManagementObjectSearcher oSearcher = new ManagementObjectSearcher(oMs, oQuery);
            ManagementObjectCollection oReturnCollection = oSearcher.Get();
            foreach( ManagementObject oReturn in oReturnCollection )
            {
                i++;
                    // Service name
           
                    msg_lbl.Text+=("<br>Name : " + oReturn["Name"].ToString());
                    //Service type
                    msg_lbl.Text += ("<br>Type: WIN32_" + oReturn["ServiceType"].ToString());   
                    // Status of the Service
                    msg_lbl.Text+=("<br>State: " + oReturn["State"].ToString());
                    //Exit code of the service
                    msg_lbl.Text+= ("<br>ExitCode: " + oReturn["ExitCode"].ToString());
                   
                 
            }
            if (i == 0)
            {
              //This msg will be displayed if the service is not a specified one
                msg_lbl.Text = "The specified service does not exist as an installed service.";
            }

  }
        catch(Exception ex)
        {
          //This msg will be displayed if the server is down or NOT TS able
                msg_lbl.Text = "The RPC server is unavailable.... ";
          }

    }

   
    protected void SelectService_CheckedChanged(object sender, EventArgs e)
    {
        if (Other_radio.Checked == true)
        {
            Service_txt.Enabled = true;
        }
    }
    protected void Other_radio_CheckedChanged(object sender, EventArgs e)
    {
        if (Other_radio.Checked == true)
        {
            Service_txt.Enabled = true;
        }

Thanks :)

nimesh 5Sep2009 21:31

Re: Check Windows System Services of Remote Machine
 
Quote:

Usually to check the service the analyst need to login into remote machine with the Domain A/c and password which takes 10-15min
It's not necessary to login to remote computer to check it's services.

if you know the user name and password you can check it directly from your machine.

open services.msc to open your machines windows services
goto Action -> Connect to another computer
give the computer name

I don't remember when to give the username/password
I used to first connect to remote machines ipc share and then use this

shabbir 3Oct2009 17:07

Re: Check Windows System Services of Remote Machine
 
Nomination for Article of the month - Sep 2009 Started. Nominate this article.


All times are GMT +5.5. The time now is 04:59.