Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WMI .NET Invalid query

Tags:

c#

.net

wmi

wql

I keep getting an "Invalid Query" exception when trying to execute the following query:

ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_DiskQuota WHERE QuotaVolume.DeviceID = 'C:'");
ManagementObjectCollection quotaCollection = searcher.Get();

However this works: "SELECT * FROM Win32_DiskQuota".

According to MSDN:

For most uses of class descriptors in a WHERE clause, WMI flags the query as invalid and returns an error. However, use the dot (.) operator for properties of type object in WMI. For example, the following query is valid if Prop is a valid property of MyClass and is type object:

SELECT * FROM MyClass WHERE Prop.embedprop = 5

Does it mean this only works if Prop declared as OBJECT?

Here are the exception details:

System.Management.ManagementException was unhandled
  HResult=-2146233087
  Message=Invalid query 
  Source=System.Management
  StackTrace:
       в System.Management.ManagementException.ThrowWithExtendedInfo(ManagementStatus errorCode)
       в System.Management.ManagementObjectCollection.ManagementObjectEnumerator.MoveNext()
       в UserQuota.Program.getQuota() в c:\users\administrator\documents\visual studio 2015\Projects\UserQuota\UserQuota\Program.cs:строка 40
       в UserQuota.Program.Main(String[] args) в c:\users\administrator\documents\visual studio 2015\Projects\UserQuota\UserQuota\Program.cs:строка 33
       в System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       в System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       в Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       в System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       в System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       в System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       в System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       в System.Threading.ThreadHelper.ThreadStart()
  InnerException: 
like image 594
Vladimir M. Avatar asked Nov 08 '22 12:11

Vladimir M.


1 Answers

Yes. According to the Win32_DiskQuota class documentation, the QuotaVolume property is a reference to a Win32_LogicalDisk WMI class. The quotation from MSDN you supplied gave the reason why the query is invalid according to the WQL specs.

Instead, you can use something like this:

ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_DiskQuota WHERE QuotaVolume = \"Win32_LogicalDisk.DeviceID=\\\"C:\\\"\"");
ManagementObjectCollection quotaCollection = searcher.Get();

(Notice all the escaping...)

like image 97
Tomer Avatar answered Nov 15 '22 13:11

Tomer