{"id":8751,"date":"2012-07-11T00:01:00","date_gmt":"2012-07-11T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2012\/07\/11\/use-powershell-and-avoid-three-gotchas-with-wql-where-clauses\/"},"modified":"2012-07-11T00:01:00","modified_gmt":"2012-07-11T00:01:00","slug":"use-powershell-and-avoid-three-gotchas-with-wql-where-clauses","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/use-powershell-and-avoid-three-gotchas-with-wql-where-clauses\/","title":{"rendered":"Use PowerShell and Avoid Three Gotcha&#039;s with WQL Where Clauses"},"content":{"rendered":"<p><b>Summary<\/b>: Microsoft Scripting Guy, Ed Wilson, shows you how to avoid three potential issues when using WQL data queries with Windows PowerShell.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Question\" \/>&nbsp;Hey, Scripting Guy! Yesterday you said there were three basic WQL keywords: <b>Select<\/b>, <b>From<\/b>, and <b>Where<\/b>. You talked about <b>Select<\/b> and <b>From<\/b>. I guess you are going to talk about <b>Where<\/b> today? Am I right?<\/p>\n<p>&mdash;BJ<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Answer\" \/>&nbsp;Hello BJ,<\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. The good news is that it is not nearly as hot today as it was yesterday. The bad news is that the humidity is hovering around 80 percent, and that makes it 87 degrees Fahrenheit (30.5 degrees Celsius according to my <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2010\/02\/21\/hey-scripting-guy-february-21-2010.aspx\" target=\"_blank\">conversion module<\/a>). Last night, the Scripting Wife, Brian from the Charlotte PowerShell User Group, and I went out to celebrate me turning in the last&nbsp; chapter of my Windows PowerShell&nbsp;3.0 Step By Book that will be published by Microsoft Press (<a href=\"https:\/\/aka.ms\/PowerShell3SBS\" target=\"_blank\">preorder from Amazon is available now<\/a>). Brian volunteered to do a peer review for my next book. Pretty exciting stuff.<\/p>\n<p>Yes, BJ, today we need to talk about the WQL <b>Where<\/b><i> <\/i>statement.<\/p>\n<p style=\"padding-left: 30px\"><b>Note&nbsp;&nbsp;&nbsp;<\/b>This is the second blog in a series about using the WMI Query language. You should definitely read the first blog in the series before continuing today. Yesterday, I talked about <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2012\/07\/10\/three-easy-ways-to-use-powershell-and-wql-to-get-wmi-data.aspx\" target=\"_blank\">Three Easy Ways to Use PowerShell and WQL to Get WMI Data<\/a>.<\/p>\n<h2>Using the basic WQL Where<i> <\/i>statement<\/h2>\n<p>Typing the <b>Where<\/b><i> <\/i>statement in a WQL query is easy; it is what comes after it that can be a bit of a bother. There are three main gotcha&rsquo;s when it comes to working with the WQL <b>Where<\/b><i> <\/i>statement. These problem areas are listed here.<\/p>\n<ol>\n<li>You need to know the format of the value to use in creating the filter.<\/li>\n<li>You need to know the exact property name that you want to use.<\/li>\n<li>You need to know the correct operator to use.<\/li>\n<\/ol>\n<p>With these three concerns, you can surmise that there are three parts in the basic <b>Where<\/b><i> <\/i>statement (four parts if you add in the <b>Where<\/b><i> <\/i>keyword). These parts are shown here.<\/p>\n<table border=\"1\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td width=\"160\" valign=\"top\">\n<p><b><i>Where <\/i><\/b><b><\/b><\/p>\n<\/td>\n<td width=\"160\" valign=\"top\">\n<p><b>Property name<\/b><\/p>\n<\/td>\n<td width=\"160\" valign=\"top\">\n<p><b>Operator<\/b><\/p>\n<\/td>\n<td width=\"160\" valign=\"top\">\n<p><b>Value<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td width=\"160\" valign=\"top\">\n<p>Where<\/p>\n<\/td>\n<td width=\"160\" valign=\"top\">\n<p>Name<\/p>\n<\/td>\n<td width=\"160\" valign=\"top\">\n<p>=<\/p>\n<\/td>\n<td width=\"160\" valign=\"top\">\n<p>&ldquo;notepad&rdquo;<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2>Gotcha #1: What does that value look like?<\/h2>\n<p>The following code provides an example of using the above <b>Where<\/b><i> <\/i>clause in an actual WQL query:<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; $name = &#8220;Select * from win32_Process where name = &#8216;notepad'&#8221;<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; Get-WmiObject -Query $name<\/p>\n<p>Interestingly enough, this code also illustrates <i>Gotcha <\/i>#3: You need to know the format of the value to use in creating the filter. For example, when the previous command runs, no data returns. Because the first command starts an instance of Notepad, the query <i>should <\/i>return data. But it does not.<\/p>\n<p style=\"padding-left: 30px\"><b>Note&nbsp;&nbsp;&nbsp;<\/b>When working with WMI, and especially with WQL, do not forget that you are also using Windows PowerShell. Often if a WQL query does not work as expected, the results are more easily obtainable via &ldquo;standard&rdquo; Windows PowerShell techniques. Unless you are returning massive amounts of data from across bandwidth-constrained remote systems, it is rarely cost effective, in terms of productivity, to waste hours of your time trying to perfect a complicated and convoluted WQL query when there is a perfectly acceptable &ldquo;pure Windows PowerShell&rdquo; alternative.<\/p>\n<p>As a quick check, I retrieve all instances of the entire <b>Win32_Process<\/b> object, and then I use the <b>Select-Object <\/b>cmdlet to choose only the name of the process. Then I use <b>Where-Object<\/b> to find processes with a name that contains the letters &ldquo;notepad&rdquo; in the process name. The code is shown here:<\/p>\n<p style=\"padding-left: 30px\">notepad<\/p>\n<p style=\"padding-left: 30px\">$name = &#8220;Select * from win32_Process where name = &#8216;notepad'&#8221;<\/p>\n<p style=\"padding-left: 30px\">gwmi -Query $name<\/p>\n<p style=\"padding-left: 30px\">gwmi win32_process | select name | where { $_.name -match &#8216;notepad&#8217;}<\/p>\n<p>The code and resulting output are shown in the image that follows.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/3644.HSG-7-11-12-01.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/3644.HSG-7-11-12-01.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>Ah ha! The values that WMI expect for the name property include the file extension. When the query changes to include the .exe extension in the file name, the query works as expected. The revised code is shown here:<\/p>\n<p style=\"padding-left: 30px\">$name = &#8220;Select * from win32_Process where name = &#8216;notepad.exe'&#8221;<\/p>\n<p style=\"padding-left: 30px\">gwmi -Query $name<\/p>\n<p>The query and the associated results are shown in the image that follows.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/6177.HSG-7-11-12-02.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/6177.HSG-7-11-12-02.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<h2>Gotcha #2: What&rsquo;s the property name I need?<\/h2>\n<p>One problem with using the <b>Where<\/b><i> <\/i>statement to filter the number of returned instances, is that you need to know the property name. In addition to just getting a valid property name, you need to get the property name to return the sort of data that you want. For example, the following query does not return any instances:<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; $service = &#8220;Select name from win32_service where status = &#8216;running'&#8221;<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; get-wmiobject -Query $service<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt;<\/p>\n<p>It is possible to use the <b>Get-Member<\/b> cmdlet to view the properties of interest. An example of this technique is shown here:<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; Get-WmiObject win32_service | Get-Member -MemberType property s*<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp; TypeName: System.Management.ManagementObject#root\\cimv2\\Win32_Service<\/p>\n<p style=\"padding-left: 30px\">Name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; MemberType Definition<\/p>\n<p style=\"padding-left: 30px\">&#8212;-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8212;&#8212;&#8212;- &#8212;&#8212;&#8212;-<\/p>\n<p style=\"padding-left: 30px\">ServiceSpecificExitCode Property&nbsp;&nbsp; System.UInt32 ServiceSpecificExitCode {get;set;}<\/p>\n<p style=\"padding-left: 30px\">ServiceType&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Property&nbsp;&nbsp; System.String ServiceType {get;set;}<\/p>\n<p style=\"padding-left: 30px\">Started&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Property&nbsp;&nbsp; System.Boolean Started {get;set;}<\/p>\n<p style=\"padding-left: 30px\">StartMode&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Property&nbsp;&nbsp; System.String StartMode {get;set;}<\/p>\n<p style=\"padding-left: 30px\">StartName&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Property&nbsp;&nbsp; System.String StartName {get;set;}<\/p>\n<p style=\"padding-left: 30px\">State&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Property&nbsp;&nbsp; System.String State {get;set;}<\/p>\n<p style=\"padding-left: 30px\">Status&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Property&nbsp;&nbsp; System.String Status {get;set;}<\/p>\n<p style=\"padding-left: 30px\">SystemCreationClassName Property&nbsp;&nbsp; System.String SystemCreationClassName {get;set;}<\/p>\n<p style=\"padding-left: 30px\">SystemName&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Property&nbsp;&nbsp; System.String SystemName {get;set;}<\/p>\n<p>The problem is distinguishing the difference between <b>State<\/b><i> <\/i>and <b>Status<\/b><i>. <\/i>Although it is true that looking up the WMI class on MSDN will provide the answer, it is also true that Windows PowerShell can often provide answers faster and easier than opening a browser, navigating to MSDN, looking up the WMI class, finding the article, and reviewing the documentation. Sometimes answers are as easy as piping the results of a WMI query to the <b>Format-List<\/b> cmdlet as shown here:<\/p>\n<p style=\"padding-left: 30px\">Get-WmiObject win32_service | Format-List *<\/p>\n<p>The output from the command is shown here:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/4721.HSG-7-11-12-03.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/4721.HSG-7-11-12-03.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>So, the problem with the previous query was choosing the wrong<i> <\/i>property. To find out if a service is running or not, use the <b>State<\/b> property, not the <b>Status<\/b> property. This is an easy mistake to make, which is alleviated when a sampling of the properties and their associated values is made. Here is the revised query:<\/p>\n<p style=\"padding-left: 30px\">$service = &#8220;Select name from win32_service where state = &#8216;running'&#8221;<\/p>\n<p style=\"padding-left: 30px\">get-wmiobject -Query $service<\/p>\n<h2>Gotcha #3: What does the operator look like?<\/h2>\n<p>This might not be a gotcha if it were not for the fact that Windows PowerShell uses distinct operators. In fact, the WQL operators appear to be much more &ldquo;standard&rdquo; than those used by Windows PowerShell. The issue, of course, is that when using Windows PowerShell to do WMI, using WQL mixed operators often enter the equation. Here is a table of WQL comparison operators:<\/p>\n<table border=\"1\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td width=\"283\" valign=\"top\">\n<p class=\"TableHead\"><strong>Operator<\/strong><\/p>\n<\/td>\n<td width=\"283\" valign=\"top\">\n<p class=\"TableHead\"><strong>Meaning<\/strong><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td width=\"283\" valign=\"top\">\n<p class=\"TableText\">=<\/p>\n<\/td>\n<td width=\"283\" valign=\"top\">\n<p class=\"TableText\">Equal<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td width=\"283\" valign=\"top\">\n<p class=\"TableText\">&lt;&gt;&nbsp;<\/p>\n<\/td>\n<td width=\"283\" valign=\"top\">\n<p class=\"TableText\">Not equal<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td width=\"283\" valign=\"top\">\n<p class=\"TableText\">&lt;&nbsp;<\/p>\n<\/td>\n<td width=\"283\" valign=\"top\">\n<p class=\"TableText\">Less than<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td width=\"283\" valign=\"top\">\n<p class=\"TableText\">&gt;&nbsp;<\/p>\n<\/td>\n<td width=\"283\" valign=\"top\">\n<p class=\"TableText\">Greater than<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td width=\"283\" valign=\"top\">\n<p class=\"TableText\">&lt;=<\/p>\n<\/td>\n<td width=\"283\" valign=\"top\">\n<p class=\"TableText\">Less than or equal<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td width=\"283\" valign=\"top\">\n<p class=\"TableText\">&gt;=<\/p>\n<\/td>\n<td width=\"283\" valign=\"top\">\n<p class=\"TableText\">Greater than or equal<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td width=\"283\" valign=\"top\">\n<p class=\"TableText\">!=<\/p>\n<\/td>\n<td width=\"283\" valign=\"top\">\n<p class=\"TableText\">Not equal<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>There are other operators, but these are used for making comparisons. The following query selects the name and the priority from processes (<b>Win32_Process<\/b>) where the priority of the process is greater than or equal to 11. The <b>Get-WmiObject<\/b> cmdlet runs the query. The code is shown here:<\/p>\n<p style=\"padding-left: 30px\">$highPriority = &#8220;Select name, priority from Win32_Process where priority &gt;= 11&#8221;<\/p>\n<p style=\"padding-left: 30px\">Get-WmiObject -Query $highPriority<\/p>\n<p>The query and the output associated with the query are shown in the following image.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2425.HSG-7-11-12-04.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2425.HSG-7-11-12-04.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>BJ, that is all there is to avoiding three main gotcha&rsquo;s when using the <b>Where<\/b> clause in WQL. WQL Week will continue tomorrow when I will talk more about using the <b>Where<\/b> clause.<\/p>\n<p>I invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p><b>Ed Wilson, Microsoft Scripting Guy<\/b>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, shows you how to avoid three potential issues when using WQL data queries with Windows PowerShell. &nbsp;Hey, Scripting Guy! Yesterday you said there were three basic WQL keywords: Select, From, and Where. You talked about Select and From. I guess you are going to talk about Where today? Am [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[3,4,45,6],"class_list":["post-8751","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-scripting-guy","tag-scripting-techniques","tag-windows-powershell","tag-wmi"],"acf":[],"blog_post_summary":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, shows you how to avoid three potential issues when using WQL data queries with Windows PowerShell. &nbsp;Hey, Scripting Guy! Yesterday you said there were three basic WQL keywords: Select, From, and Where. You talked about Select and From. I guess you are going to talk about Where today? Am [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/8751","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=8751"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/8751\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=8751"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=8751"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=8751"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}