yes Willy it is.
it is just that using System.Management does not collect all the field of WMI.
i was just hoping to use FrameWork over WMI.
i do have another problem. i figured how to get to the remote machine and
start the data collection but it runs then stops when reading the "DNS"
service and the error in VS C# 2005 does not tell me what the problem is. it
is very generic message stating "Get general help for this exception" when i
do a ManagementObject.wm.Get();
public string[] getXprServices(bool oRunning)
{
///once all the service is collected return the full array to
display to monitor.
///the way to get the detail i want requires that i call both
the framework
///and the wmi.
//for debug only!!
string strmachine = "server2";
ConnectionOptions options = new ConnectionOptions();
options.Username = "usera";
options.Password = "downriver";
options.Authority = "ntdlmdomain:mydomain";
ServiceController[] xprServices;
xprServices = ServiceController.GetServices(strmachine);
string[,] aServices = new string[xprServices.Length + 1, 5];
int iElement = 1; //the service number incrementor
aServices[0, 0] = "Service Name";
aServices[0, 1] = "Display Name";
aServices[0, 2] = "Status";
aServices[0, 3] = "Log on As";
aServices[0, 4] = "# of Dependencies";
foreach (ServiceController sServices in xprServices)
{
if (oRunning) //only collect the running services
{
if (sServices.Status == ServiceControllerStatus.Running)
{
aServices[iElement, 0] = sServices.ServiceName;
aServices[iElement, 1] = sServices.DisplayName;
aServices[iElement, 2] = sServices.Status.ToString();
ManagementObject wmiService;
wmiService = new
ManagementObject("Win32_Service.Name='" + sServices.ServiceName + "'");
wmiService.Get();
aServices[iElement, 3] =
wmiService["StartName"].ToString();
aServices[iElement, 4] =
sServices.ServicesDependedOn.Length.ToString();
}
}
else //all services
{
aServices[iElement, 0] = sServices.ServiceName;
aServices[iElement, 1] = sServices.DisplayName;
aServices[iElement, 2] = sServices.Status.ToString();
ManagementScope scope = new ManagementScope("\\\\" +
strmachine + "\\root\\cimv2", options);
//scope.Connect();
ManagementObject wmiService;
wmiService = new ManagementObject("Win32_Service.Name='"
+ sServices.ServiceName + "'");
wmiService.Get(); <<<< dies here!!!
aServices[iElement, 3] =
wmiService["StartName"].ToString();
//aServices[iElement, 3] = "";
aServices[iElement, 4] =
sServices.ServicesDependedOn.Length.ToString();
}
iElement++;
}//foreach(ServiceController sServices in xprServices)
formatoutput fout = new formatoutput();
string[] saServices = fout.multiArrayFormat(aServices);
return (saServices);
}//public string[] getXprServices()
Willy Denoyette said:
auldh said:
is there no way via framework to see the "startname" property of the service?
do i still have to call the "WMI"?
System.Management is part of the framework isn't it?
if so do i have to use:
System.Management.ManagementScope < to the remote machine\\root\\cimv2
System.ManagementObject wmiService;
wmiService = new ManagementObject("Win32_Service.Name='" +
sServices.ServiceName + "'");
in order to get the "startname"?
i'm trying to get the details from both local and remote machines services.
You can get all properties you are interested in by using a WQL query like so:
string serviceName = "Spooler";
SelectQuery query = new SelectQuery("select StartName, Status from Win32_Service where
Name ='" + serviceName + "'");
using(ManagementObjectSearcher searcher = new ManagementObjectSearcher(query))
{
foreach (ManagementObject mo in searcher.Get()) {
Console.WriteLine("{0} - {1} ", mo["StartName"], mo["Status"]);
}
}
Sure, to connect to a remote system, you'll need to specify a ManagementScope and
appropriate ConnectionOptions. Also, you need to take care of when connecting to a remote
machine is that you specify a user account with appropriate rights to query the WMI
namespace.
Willy.