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;
}


