4

I have been using an application that queries Windows Services running on remote servers and writes the Machine Name, Service Name, and Status to a database.

However, I want to try and capture the startup type (Automatic, Manual, Disabled) as well. I was using a Service Controller which does not have any options for startup type so I started looking at using a Management Class. This class looks like it has everything I need but I don't know how to use it against my remotes servers. For the Service Controller, I was doing this:

ServiceController[] services = ServiceController.GetServices(serverIP);

foreach (ServiceController service in services)
{
   var machine = service.MachineName;
   var displayName = service.DisplayName;
   var status = service.Status;
}

I tried this for the Management class:

ManagementClass class1 = new ManagementClass(serverIP + "\\" + "Win32_Service");

foreach (ManagementObject ob in class1.GetInstances())
{
   var machine = serverIP;
   var displayName = ob.GetPropertyValue("Description");
   var name = ob.GetPropertyValue("PathName");
   var startMode = ob.GetPropertyValue("StartMode");
   var status = ob.GetPropertyValue("State");
}

But of course it didn't work. Anyone know how I can get the Services from a remote machine using the Management Class? Or is there another way using the Service Controller to get the startup type?

I also tried to combine them both and put the Management Class foreach statement inside the Service Controller but it got stuck in an endless loop.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Matt
  • 1,220
  • 3
  • 21
  • 36
  • I think I got it by doing this: (@"\\" + serverIP + "\\root\\cimv2:Win32_Service"). I will keep testing because now I can't find the Property for Service Name. – Matt Jul 15 '11 at 18:56
  • Your fix makes sense; you always refer to remote computers as \\computername, and without the @ sign C# things you're escaping a single backslash. – Andy Jul 15 '11 at 19:00
  • If anyone is wondering the Property for Service Name is just Name. – Matt Jul 15 '11 at 20:18

2 Answers2

1

The information you're looking for is available in WMI.

It will be MUCH easier to write this whole thing in PowerShell than in Pure C#. WMI code gets very messy in C# (or C++, or VBScript), very quickly. This snippet demostrates getting the data from a list of computers. To embed in C#, simply use System.Management.Automation, and add PowerShell.Create().AddScript(...).Invoke()

$computerList = "a","b","c"
Get-WmiObject -computerName $computerList -asjob
    | Wait-job
    | receive-job
    | Select-Object DisplayName, Description, StartMode, State

Hope this helps,

Start-Automating
  • 8,067
  • 2
  • 28
  • 47
  • Thank you for the reply. What if I pull my servers from a table in a database? I have created a SQL reader which pulls the server IP. Sorry I left that out of the example. – Matt Jul 15 '11 at 18:51
  • Then you'd just populate $computerList from that database. The SqlClient class in .NET should be what you're looking for. You can create it New-Object (a few web searches for PowerShell + SQL will send you on the right path). – Start-Automating Jul 16 '11 at 08:01
1

The same code above worked. Just add this to Management Class

(@"\\" + serverIP + "\\root\\cimv2:Win32_Service")
Jim Garrison
  • 85,615
  • 20
  • 155
  • 190
Matt
  • 1,220
  • 3
  • 21
  • 36