PowerShell: WMI WQL Queries – made easy for remote WMI especially for SMS 2003 or ConfigMgr 2007
Once upon a time to do a WMI query you had to use one of the following:
- WMIC
- Cumbersome
- Must be an administrator
- WBEMTEST
- Make the popup windows stop
- CIM Studio
Now we have PowerShell. PowerShell allows a lot of formatting, but check out how easy it is to query WMI:
Note: Crtl+C breaks a query in PowerShell
1) Create the Query Object
$q = New-Object System.Management.ObjectQuery
2) Enter your Query
$q.QueryString = "Select * from Win32_MemoryDevice"
3) Create your searcher and pass the query
$s = New-Object System.Management.ManagementObjectSearcher($q)
4) Set your WMI Path
$s.Scope.Path = "\root\CIMv2"
Note: the proceeding slash for local machines
5) Run the query!
$s.Get()
Grab some useful info
$s.Get() | select Caption, EndingAddress
|
Caption |
EndingAddress |
| Memory Device |
2097151 |
| Memory Device |
2097151 |
Now lets do that again for a SMS 2003 or ConfigMgr 2007 WQL query!
1) Create the Query Object
$q = New-Object System.Management.ObjectQuery
2) Enter your Query
$q.QueryString = "Select * from SMS_R_System"
3) Create your searcher and pass the query
$s = New-Object System.Management.ManagementObjectSearcher($q)
4) Set your WMI Path
$s.Scope.Path = "\\ServerName\root\sms\site_SiteCode"
Note: the remote machine name. This is how you query remote workstations or server via WMI and PowerShell
5) Run the query!
$s.Get()
Grab some useful info
$s.Get() | Select Name, ClientVersion, OperatingSystemNameandVersion
Tip: this can potentially return a ton of information. You can reduce the returns by the following.
1 | foreach {$s.get()} | select name, resourceID
Source: "/\/\o\/\/ [MVP]" http://groups.google.nl/group/microsoft.public.windows.server.scripting/browse_thread/thread/7c0e03c992d507ac/2d8706e707d27965?lnk=st&q=get-content+of+large+files.&rnum=1&hl=en#2d8706e707d27965
Summary: Doing a WQL query via PowerShell is very easy assuming you have the rights to read the WMI.