4

I want to know how to run WQL query with powershell. Here is the WQL query

Select UAR.User, UAR.Application, UAR.CurrentState from sms_fullcollectionmembership as FCM
INNER JOIN SMS_UserApplicationRequest as UAR ON UAR.User=FCM.SMSID
where FCM.CollectionID="100104"
Roxx
  • 3,738
  • 20
  • 92
  • 155

2 Answers2

5

As described in the about_WQL help file, you can either use one of these 2 cmdlets: Get-WmiObject or Get-CimInstance, or you can use the [wmisearcher] type accelerator:

Using Get-WmiObject:

 $queryNameVersion = "Select Name, Version from Win32_Bios"
 Get-WmiObject -Query $queryNameVersion

Using Get-CimInstance:

 $queryNameVersion = "Select Name, Version from Win32_Bios"
 Get-CimInstance -Query $queryNameVersion

Using [wmisearcher]:

 $searcher = [wmisearcher]"Select Name, Version from Win32_Bios"
 $searcher.Get()

Get-WmiObject is designed to work with WMI (Microsofts own CIMv2 server implementation), whereas Get-CimInstance is supposed to work with any CIMv2 compliant server (although, AFAIK, WQL is specific to WMI).


In your example, you could put the query into a here-string to maintain readability:

$SCCMQuery = @'
Select UAR.User, UAR.Application, UAR.CurrentState from sms_fullcollectionmembership as FCM
INNER JOIN SMS_UserApplicationRequest as UAR ON UAR.User=FCM.SMSID
where FCM.CollectionID="100104"
'@
$Results = Get-WmiObject -Namespace "root\SMS\Site_Name" -Query $SCCMQuery
Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206
  • Can you please have a look on this question http://stackoverflow.com/questions/31324415/double-backslash-in-wmi-wql-query-powershell – Roxx Jul 09 '15 at 17:34
2

As simple as:

Get-WmiObject -query $wqlQuery

Standard alias is gwmi.

Supply the -namespace parameter if outside the default WMI namnespace.

(There is also Get-CimInstance -query … but I'm unfamiliar with that.)

Richard
  • 106,783
  • 21
  • 203
  • 265
  • Either should work, `Get-CIMInstance` does what `Get-WMIObject` and more, so it does the same regarding WMI queries. – Vesper Jul 09 '15 at 07:40