WMI System.Management.ObjectQuery valid strings and error "User credentials cannot be used for local connections"
-
jueves, 07 de mayo de 2009 9:04I'm new to WMI and was wondering if anybody knows if there is a reference detailing the valid query strings that can be passed in to ObjectQuery().
I am trying to get the free disk space on a remote PC. I have tried using the string
select Size from Win32_LogicalDisk where DriveType=3
but get an error "User credentials cannot be used for local connections"
This is my code:
//Connection credentials to the remote computer - not needed if the logged in account has access
ConnectionOptions oConn = new ConnectionOptions();
oConn.Username = "Administrator";
oConn.Password = "pass";
System.Management.ManagementScope oMs = new System.Management.ManagementScope("\\\\192.168.0.106", oConn);
//get Fixed disk stats
System.Management.ObjectQuery oQuery = new System.Management.ObjectQuery("select Size from Win32_LogicalDisk where DriveType=3");
//Execute the query
ManagementObjectSearcher oSearcher = new ManagementObjectSearcher(oMs, oQuery);
//Get the results
ManagementObjectCollection oReturnCollection = oSearcher.Get();
I thought that perhpas the string i am passing in to ObjectQuery is incorrect.
Any ideas?
Thanks in advance.
ak268
Todas las respuestas
-
viernes, 08 de mayo de 2009 6:57ModeradorHello,
Based on the document, http://msdn.microsoft.com/en-us/library/aa394173.aspx
I think the query string looks well. The problem is we need specify the namesapce after the remote machine.
new System.Management.ManagementScope(@"\\192.168.0.106\root\cimv2", oConn);
Please have a try and let us to know if it fixes your issue.
Best regards,
Colbert
We have published a VSTO FAQ recently, you can view them from the entry thread http://social.msdn.microsoft.com/Forums/en/vsto/thread/31b1ffbf-117b-4e8f-ad38-71614437df59. If you have any feedbacks or suggestions on this FAQ, please feel free to write us emails to colbertz@microsoft.com. -
viernes, 08 de mayo de 2009 10:25Moderador
Hi Arvinder,
I just search "User credentials cannot be used for local connections" in google and find the following one,
http://www.experts-exchange.com/Programming/Languages/C_Sharp/Q_22105229.html
So I know we may need a namespace appended.
And I also search "Win32_LogicalDisk" in google and get the following link,
http://msdn.microsoft.com/en-us/library/aa394173.aspx
At the bottom of the page, we can confirm the Win32_LogicalDisk uses the \root\cimv2 namespace.
And actually this official document sample also use namespace for the remote machine name.
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _
& strComputer & "\root\cimv2")
:-)
Regards,
Colbert
We have published a VSTO FAQ recently, you can view them from the entry thread http://social.msdn.microsoft.com/Forums/en/vsto/thread/31b1ffbf-117b-4e8f-ad38-71614437df59. If you have any feedbacks or suggestions on this FAQ, please feel free to write us emails to colbertz@microsoft.com.- Marcado como respuesta ak268 viernes, 08 de mayo de 2009 10:32
-
jueves, 09 de febrero de 2012 5:49
Hi Ji Zhou,
Your link to Experts Exchange contains a hidden solution - I can't see it. Care to share the solution ("namesapce appended")?
Thanks.
-
jueves, 09 de febrero de 2012 5:57
I get the "user credentials cannot be used for local connections" issue when I call "Get" below. How do I fix this?
ConnectionOptions options = new ConnectionOptions();
string[] propertiesToSelect = new[] { "Handle", "ProcessId", "ExecutablePath" };
options.Username = "USERNAME"
options.Password = "PASSWORD";
options.EnablePrivileges = true;
options.Impersonation = ImpersonationLevel.Impersonate;
ManagementScope managementScope = new ManagementScope();
managementScope.Options = options;
SelectQuery processQuery = new SelectQuery("Win32_Process", string.Format("Name = '{0}'", processName), propertiesToSelect);
//ManagementScope
using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(managementScope, processQuery))
{
using (ManagementObjectCollection processes = searcher.Get()) -
martes, 13 de marzo de 2012 20:09
Having the same issue, and at the moment I'm of the mind that it's a rather fatal bug in WMI. It's a bug because my local machine is running IIS under a different set of credentials (an actual Windows User account that's been designated as the account for Application Pools, not NETWORK SERVICE or any of the other IIS defaults), and I need to supercede those credentials to get permission(s) to do stuff via WMI.
If I can find a way to get around this nonsense I'll be back to post the solution.
Seriously, this should be considered a bug, and MS should fix it.
Jerry H.
-
lunes, 30 de abril de 2012 6:38
I am also getting same problem. Impersonation is working on other machine but it is not working in local machine. Did any one find solution to impersonate WMI call on local machine..
Thanks.

