how to use "ServiceController.MachineName Property "

G

Guest

hello,
i'm try to research the way to open a remote machine's to collect the
services and processes. i have the local data collection running but i can
not figure out what MSDN or the C# help file is stating how to use
"servicecollecter.machinename" to get the remote server's data.

there is no samples on using framework to do this but loads of WMI.

can someone share any sample / snippet on using framework with C# 2005 to
get the remote server's services and or processes?
 
W

Willy Denoyette [MVP]

auldh said:
hello,
i'm try to research the way to open a remote machine's to collect the
services and processes. i have the local data collection running but i can
not figure out what MSDN or the C# help file is stating how to use
"servicecollecter.machinename" to get the remote server's data.

there is no samples on using framework to do this but loads of WMI.

can someone share any sample / snippet on using framework with C# 2005 to
get the remote server's services and or processes?



Don't know what docs you are looking at, but the ".NET Framework Class Library" in MSDN is
where to start, there is the static ServiceController.GetServices Method that takes the
machine name (a string) you want to get a list of services from.
Note however that this requires you to run in the account of an admin on the remote system.

Willy.
 
G

Guest

thanks Willy that was easy.
i was reading the "ServiceController Members" and found
the "machinename" properties.
 
G

Guest

is there no way via framework to see the "startname" property of the service?
do i still have to call the "WMI"?
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.
 
W

Willy Denoyette [MVP]

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.
 
G

Guest

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.
 
W

Willy Denoyette [MVP]

auldh said:
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.

Sure it does, System.Management is a thin layer over WMI, what fields do you think are not
collected?
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();

If you have problems with a single service, run wbemtest.exe and try to find out what's
wrong.



Willy.
 
G

Guest

i'm not having luck running "wbemtest.exe" i'm able to get to the machine and
all.
i would use WMI solely but i would like to collect the "ServicesDependedOn"
and maybe other objects in the "servicecontroller" set.

when i review the exception box that comes up it states that it is a wrong
cast? but this was successful when ran through the local machine. could it be
a timeout? i'm not sure what to do at this point. i provided the function in
my previous attachment. it runs well on the local machine with out a problem
but only when a remote machine is called....

lost...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top