«

»

Nov 22

Print this Post

Get window service information by WMI Query

How to find Window Service Name/ Service Path/ Service State for local or remote machine/server by using WMI?

VBScript:

arrComputers = Array(“localhost”)
For Each strComputer In arrComputers
WScript.Echo
WScript.Echo “==========================================”
WScript.Echo “Computer: ” & strComputer
WScript.Echo “==========================================”

Set objWMIService = GetObject(“winmgmts:\\” & strComputer & “\root\CIMV2″)
Set colItems = objWMIService.ExecQuery( _
“SELECT * FROM Win32_Service”,,48)
For Each objItem in colItems
Wscript.Echo “———————————–”
Wscript.Echo “Win32_Service instance”
Wscript.Echo “———————————–”
Wscript.Echo “Caption: ” & objItem.Caption
Wscript.Echo “Description: ” & objItem.Description
Wscript.Echo “DisplayName: ” & objItem.DisplayName
Wscript.Echo “Name: ” & objItem.Name
Wscript.Echo “PathName: ” & objItem.PathName
Wscript.Echo “StartMode: ” & objItem.StartMode
Wscript.Echo “State: ” & objItem.State
Wscript.Echo “Status: ” & objItem.Status
Wscript.Echo “SystemName: ” & objItem.SystemName
Next
Next

Program in C#:

using System;
using System.Management;
using System.Windows.Forms;

namespace SampleProgram
{
public class SampleQuery
{
public static void Main()
{
try
{
string[] arrComputers = {“localhost”};
foreach (string strComputer in arrComputers)
{
Console.WriteLine(“==========================================”);
Console.WriteLine(“Computer: ” + strComputer);
Console.WriteLine(“==========================================”);

ManagementObjectSearcher searcher =

new ManagementObjectSearcher(“\\\\” + strComputer + “\\root\\CIMV2″,¬†“SELECT * FROM Win32_Service”);

foreach (ManagementObject queryObj in searcher.Get())
{
Console.WriteLine(“———————————–”);
Console.WriteLine(“Win32_Service instance”);
Console.WriteLine(“———————————–”);
Console.WriteLine(“Caption: {0}”, queryObj["Caption"]);
Console.WriteLine(“Description: {0}”, queryObj["Description"]);
Console.WriteLine(“DisplayName: {0}”, queryObj["DisplayName"]);
Console.WriteLine(“Name: {0}”, queryObj["Name"]);
Console.WriteLine(“PathName: {0}”, queryObj["PathName"]);
Console.WriteLine(“StartMode: {0}”, queryObj["StartMode"]);
Console.WriteLine(“State: {0}”, queryObj["State"]);
Console.WriteLine(“Status: {0}”, queryObj["Status"]);
Console.WriteLine(“SystemName: {0}”, queryObj["SystemName"]);
}
}
}
catch(ManagementException err)
{
MessageBox.Show(“An error occurred while querying for WMI data: ” + err.Message);
}
}
}
}

output: Windows service information by wmi

———————————–
Win32_Service instance
———————————–
Caption: Windows Error Reporting Service
Description: Allows errors to be reported when programs stop working or respondi
ng and allows existing solutions to be delivered. Also allows logs to be generat
ed for diagnostic and repair services. If this service is stopped, error reporti
ng might not work correctly and results of diagnostic services and repairs might
not be displayed.
DisplayName: Windows Error Reporting Service
Name: WerSvc
PathName: C:\Windows\System32\svchost.exe -k WerSvcGroup
StartMode: Manual
State: Stopped
Status: OK
SystemName: Server01

Permanent link to this article: http://www.techxpress.in/get-window-service-information-by-wmi-query/

Bookmark this page