Check Windows System Services of Remote Machine

Discussion in 'ASP' started by naimish, Sep 3, 2009.

  1. naimish

    naimish New Member

    Joined:
    Jun 29, 2009
    Messages:
    1,043
    Likes Received:
    18
    Trophy Points:
    0
    Occupation:
    Software Engineer
    Location:
    On Earth

    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 = "[EMAIL="P@ssw0rd"]P@ssw0rd[/EMAIL]";
                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 :)
     
  2. nimesh

    nimesh New Member

    Joined:
    Apr 13, 2009
    Messages:
    769
    Likes Received:
    20
    Trophy Points:
    0
    Occupation:
    Oracle Apps Admin
    Location:
    Mumbai
    Home Page:
    http://techiethakkar.blogspot.com
    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
     
  3. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice