Error retrieving status of services using WMI

deepaklakhani
deepaklakhani used Ask the Experts™
on
I get following error when i try to retrieve the list of running services using WMI.
Error: "Provider load failure"    

I have included the code below.

Note: The code works fine if I replace the select string with [@"SELECT * FROM Win32_Directory "]


//*************CODE Starts here

ManagementObjectSearcher query;
ManagementObjectCollection queryCollection;

ManagementScope msc = new ManagementScope("\\root\\cimv2");
msc.Options.EnablePrivileges = true;
msc.Options.Impersonation =
System.Management.ImpersonationLevel.Impersonate;

string queryString = @"SELECT * FROM Win32_Service";
SelectQuery q = new SelectQuery(queryString);

query = new ManagementObjectSearcher(msc, q);
queryCollection = query.Get();

foreach( ManagementObject mo in queryCollection )
{
Console.WriteLine("Service: "+mo["Name"] );
}
//*************CODE ENDS HERE


I tried various samples, and with every sample the code fails when I try to retrieve services.

Any help will be greatly appreciated.

Thanks.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
Why cannot you use the servicecontroller component?

Dim myController As ServiceController

AllServices = ServiceController.GetServices()
Dim mnuServices(AllServices.Length + 1)
For i = 0 To AllServices.Length - 1
  AllServices(i).DisplayName
Next

Author

Commented:
daveky,

I have no control over code and this code works on other terminal. I have recommended your suggestion to the author of the code.

Author

Commented:
daveky,

I have no control over code and this code works on other terminal. I have recommended your suggestion to the author of the code.

Author

Commented:
Is there a workaround to access the services (using above code) running on a remote machine....

note: i tried using:
servicecontroller.getservices("remotemachinename")

Remotemachinename can be a machine in another domain. Is there a way to specify username/password before calling getservices method.

Deepak
Most Valuable Expert 2012
Top Expert 2008

Commented:
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Answered by daveky

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

TheLearnedOne
EE Cleanup Volunteer

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial