{"id":15341,"date":"2011-03-12T00:01:00","date_gmt":"2011-03-12T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2011\/03\/12\/explore-wmi-methods-and-properties-via-powershell-script\/"},"modified":"2023-02-17T02:16:58","modified_gmt":"2023-02-17T10:16:58","slug":"explore-wmi-methods-and-properties-via-powershell-script","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/explore-wmi-methods-and-properties-via-powershell-script\/","title":{"rendered":"Explore WMI Methods and Properties via PowerShell Script"},"content":{"rendered":"<p><b>Summary<\/b>: Microsoft Scripting Guy, Ed Wilson, illustrates how to explore WMI methods and writable properties from a Windows PowerShell script.<\/p>\n<h3>Weekend Scripter<\/h3>\n<p>Microsoft Scripting Guy, Ed Wilson, here. One of the things I like about Windows PowerShell is the ease in which I can modify things, experiment with things, play with things, and finally end up with a decent script. It is a rainy day in <a href=\"http:\/\/en.wikipedia.org\/wiki\/Charlotte,_North_Carolina\" target=\"_blank\" rel=\"noopener\">Charlotte<\/a>, <a href=\"http:\/\/en.wikipedia.org\/wiki\/North_Carolina\" target=\"_blank\" rel=\"noopener\">North Carolina<\/a>, and it is beginning to actually look like spring outside. I am listening to <a href=\"http:\/\/en.wikipedia.org\/wiki\/Radiohead\" target=\"_blank\" rel=\"noopener\">Radiohead<\/a> on my <a href=\"http:\/\/zune.net\/en-us\/products\/zunehd\/default.htm\" target=\"_blank\" rel=\"noopener\">Zune HD<\/a>, and sipping a cup of organic mint tea\u2026it is just one of those sort of laid-back days.<\/p>\n<p>I had an extremely busy week, with a presentation to the <a href=\"http:\/\/www.carolinait.org\/default.aspx\" target=\"_blank\" rel=\"noopener\">Charlotte IT Professionals Group<\/a> thrown in for fun. I absolutely love speaking to user groups, either in person or via <a href=\"http:\/\/office.microsoft.com\/en-us\/help\/microsoft-office-live-meeting-resource-center-HA010238900.aspx?CTT=1\" target=\"_blank\" rel=\"noopener\">Live Meeting<\/a>, and it is always the highlight of my week. I firmly believe and wish to support these groups because they are the embodiment of community. By the way, if you are looking for a Windows PowerShell user group in your area, check out the <a href=\"http:\/\/powershellgroup.org\/\" target=\"_blank\" rel=\"noopener\">PowerShell Group<\/a> site for listings. If you know of a group that is not listed in this directory, please add it. There is also help available if you want to start a user group.<\/p>\n<p>One of the goals of a good scriptwriter should be to write reusable code. In Windows PowerShell, this generally means creating functions. When I created the Get-WmiClassesMethods.ps1 Windows PowerShell script and the Get-WmiClassProperties.ps1 script, I had code reuse in mind. For details about the essential logic of today\u2019s script, refer to the following articles:<\/p>\n<p><a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2011\/03\/10\/use-powershell-to-find-wmi-classes-that-contain-methods.aspx\" target=\"_blank\" rel=\"noopener\">Use PowerShell to Find WMI Classes that Contain Methods<\/a><\/p>\n<p><a href=\"http:\/\/blogs.technet.com\/controlpanel\/blogs\/posteditor.aspx?SelectedNavItem=Posts&amp;WeblogID=7618&amp;WeblogPostID=3393226\" target=\"_blank\" rel=\"noopener\">Use PowerShell to Find Writable WMI Properties<\/a><\/p>\n<p>Therefore, I encapsulated the main logic into individual functions. Due to time constraints, the functions are not as reusable as I would like them to be, but I did have reuse in mind at the time. This also illustrates that there is often a tradeoff between code reuse and development time. It takes time to design a completely portable function, and sometimes it takes a long time to analyze how a function might be reused and to abstract everything.<\/p>\n<p>On a rainy Saturday morning, I thought it would be a good idea to combine the two scripts into a single script that produces a consolidated listing of implemented methods and writable properties from all the WMI classes in a particular WMI namespace. I know I can use such a list, and I hope you find it useful as well.<\/p>\n<p>To be sure, the script is a bit complicated, and it is most certainly long. That is why I uploaded the <a href=\"http:\/\/gallery.technet.microsoft.com\/scriptcenter\/10670e14-4cf1-4ce5-99d0-fc4ca80dac2c\" target=\"_blank\" rel=\"noopener\">Get-WmiClassMethodsAndWritableWmiProperties.ps1 script<\/a> to the <a href=\"http:\/\/gallery.technet.microsoft.com\/scriptcenter\/10670e14-4cf1-4ce5-99d0-fc4ca80dac2c\" target=\"_blank\" rel=\"noopener\">Scripting Guys Script Repository<\/a>. But the script is shorter and easier to understand that a corresponding script written in VBScript. In fact, I always wanted to write such a script in VBScript but I never got around to doing it\u2014and <a href=\"http:\/\/www.amazon.com\/gp\/product\/0735622310?ie=UTF8&amp;tag=tn-script-20&amp;linkCode=as2&amp;camp=1789&amp;creative=390957&amp;creativeASIN=0735622310\" target=\"_blank\" rel=\"noopener\">I wrote the book on VBScript and WMI<\/a>.<\/p>\n<p>All right, I want to start with the <b>Get-WmiClassMethods<\/b> function. I will highlight the changes I made. The first change I made was to add a <i>class<\/i> parameter to the <i>Param<\/i> portion of the function. The reason for this is that I want to collect the WMI classes outside of the function, and pass the individual WMI class information to the function for processing. This will enable me to work on the same class and to retrieve methods and properties as appropriate. Because I am going to pass the class information to the functions, I do not need to collect the classes inside the function. Therefore, I comment out the lines in the function that perform the class collection duties. This revised portion of the code is shown here.<\/p>\n<pre class=\"prettyprint language-default\"><code class=\"language-default\">Function Get-WmiClassMethods\r\n{\r\n Param(\r\n\r\n   [string]$namespace = \u201croot\\cimv2\u201d,\r\n\r\n   [string]$computer = \u201c.\u201d,\r\n\r\n   $class\r\n\r\n)\r\n\r\n $abstract = $false\r\n\r\n $method = $null\r\n\r\n #$classes = Get-WmiObject -List -Namespace $namespace | Where-Object { $_.methods }\r\n\r\n #Foreach($class in $classes)\r\n\r\n #{<\/code><\/pre>\n<p>I changed the output to include the <a href=\"http:\/\/gallery.technet.microsoft.com\/ScriptCenter\/en-us\/site\/search?f%5B0%5D.Type=RootCategory&amp;f%5B0%5D.Value=office&amp;f%5B0%5D.Text=Office&amp;f%5B1%5D.Type=SubCategory&amp;f%5B1%5D.Value=word&amp;f%5B1%5D.Text=Microsoft%20Word\" target=\"_blank\" rel=\"noopener\">Word<\/a> METHODS in addition to the WMI class name. To do this, I used the <a href=\"http:\/\/blogs.technet.com\/search\/searchresults.aspx?q=%22new-underline%22&amp;sections=7618\" target=\"_blank\" rel=\"noopener\">New-Underline function<\/a>. One of the cool things about this function, and one reason I use it so often, is that it allows one to specify the character to use for underlining, in addition to the colors to use for the text and the underline string.<\/p>\n<p>I also had to comment out the closing curly bracket (closing braces\u2026squiggly things\u2026whatever) of the <i>foreach<\/i> class loop. The comments that I included when I originally created the script make lining up the curly bracket easier. This portion of the script is shown here.<\/p>\n<pre class=\"prettyprint language-default\"><code class=\"language-default\">if($method)\r\n\r\n        {\r\n\r\n         New-Underline -strIN $class.name\r\n\r\n         New-Underline \u201cMETHODS\u201d -char \u201c-\u201c\r\n\r\n        }\r\n\r\n      $method\r\n\r\n    } #end if not abstract\r\n\r\n  $abstract = $false\r\n\r\n  $method = $null\r\n\r\n# } \r\n#end foreach class<\/code><\/pre>\n<p>Because the two functions (<b>Get-WmiClassMethods<\/b> and <b>Get-WmiClassProperties<\/b>) are written in a similar style, similar changes are required for inclusion here. I need to add a <i>class<\/i> parameter to the <i>Param<\/i> section, and I need to comment out the code that gathers the WMI classes and implements the <i>foreach <\/i>class loop. The revised code is shown here.<\/p>\n<pre class=\"prettyprint language-default\"><code class=\"language-default\">Function Get-WmiClassProperties\r\n\r\n{\r\n\r\n Param(\r\n\r\n   [string]$namespace = \u201croot\\cimv2\u201d,\r\n\r\n   [string]$computer = \u201c.\u201d,\r\n\r\n   $class\r\n\r\n)\r\n\r\n $abstract = $false\r\n\r\n $property = $null\r\n\r\n #$classes = Get-WmiObject -List -Namespace $namespace\r\n\r\n #Foreach($class in $classes)\r\n\r\n #{<\/code><\/pre>\n<p>I add the word PROPERTIES to the output portion of the script, and remove the closing curly bracket. This revision is shown here.<\/p>\n<pre class=\"prettyprint language-default\"><code class=\"language-default\">if($property)\r\n\r\n        {\r\n\r\n         New-Underline -strIN $class.name\r\n\r\n         New-Underline \u201cPROPERTIES\u201d -char \u201c-\u201c\r\n\r\n        }\r\n\r\n      $property\r\n\r\n    } #end if not abstract\r\n\r\n  $abstract = $false\r\n\r\n  $property = $null\r\n\r\n# } #end foreach class\r\n\r\n} #end function Get-WmiClassProperties<\/code><\/pre>\n<p>I moved the collection of the WMI classes, and the <i>foreach $class <\/i>construction to the entry point of the script. In addition, I added the &#8211;<i>class<\/i> parameter when I call each function. This portion of the script is shown here.<\/p>\n<p class=\"CodeBlock\" style=\"line-height: 10pt; list-style-type: disc; margin: 4pt 0in 7pt;\"><span style=\"font-family: Lucida Sans Typewriter;\"><span style=\"color: #000000; font-size: 10pt;\">\u00a0<\/span><\/span><\/p>\n<pre class=\"prettyprint language-default\"><code class=\"language-default\"># *** Entry Point to Script ***\r\n\r\n \r\n\r\n$classes = Get-WmiObject -List -Namespace $namespace\r\n\r\nForeach($class in $classes)\r\n\r\n {\r\n\r\n  Get-WmiClassMethods -class $class\r\n\r\n  Get-WmiClassProperties -class $class\r\n\r\n }<\/code><\/pre>\n<p>When the script runs, the output that is shown in the following image appears in the Windows PowerShell ISE.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/7115.WES-3-12-11-01_1E0A0A64.jpg\"><img decoding=\"async\" style=\"padding-left: 0px; padding-right: 0px; padding-top: 0px; border: 0px;\" title=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/0020.WES-3-12-11-01_thumb_13893644.jpg\" alt=\"Image of command output\" width=\"604\" height=\"432\" border=\"0\" \/><\/a><\/p>\n<p>I invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\" rel=\"noopener\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\" rel=\"noopener\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"mailto:scripter@microsoft.com\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\" rel=\"noopener\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p><b>Ed Wilson, Microsoft Scripting Guy<\/b><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, illustrates how to explore WMI methods and writable properties from a Windows PowerShell script. Weekend Scripter Microsoft Scripting Guy, Ed Wilson, here. One of the things I like about Windows PowerShell is the ease in which I can modify things, experiment with things, play with things, and finally end [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[3,4,61,45,6],"class_list":["post-15341","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-scripting-guy","tag-scripting-techniques","tag-weekend-scripter","tag-windows-powershell","tag-wmi"],"acf":[],"blog_post_summary":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, illustrates how to explore WMI methods and writable properties from a Windows PowerShell script. Weekend Scripter Microsoft Scripting Guy, Ed Wilson, here. One of the things I like about Windows PowerShell is the ease in which I can modify things, experiment with things, play with things, and finally end [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/15341","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\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=15341"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/15341\/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=15341"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=15341"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=15341"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}