{"id":12301,"date":"2011-10-24T00:01:00","date_gmt":"2011-10-24T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2011\/10\/24\/a-powershell-wmi-helper-module-described\/"},"modified":"2011-10-24T00:01:00","modified_gmt":"2011-10-24T00:01:00","slug":"a-powershell-wmi-helper-module-described","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/a-powershell-wmi-helper-module-described\/","title":{"rendered":"A PowerShell WMI Helper Module Described"},"content":{"rendered":"<p><strong>Summary:<\/strong> In this article, Microsoft Scripting Guy Ed Wilson begins part 1 of a multipart WMI helper function module for Windows PowerShell.<\/p>\n<p>&nbsp;<\/p>\n<p>Microsoft Scripting Guy Ed Wilson here. While I was teaching my Windows PowerShell Best Practices class in Montreal, we spent an entire day talking about Windows Management Instrumentation (WMI). While WMI seems to have a &ldquo;bad reputation&rdquo; in terms of complexity, consistency, and discoverability, Windows PowerShell has done much to make WMI more consistent, less complex, and definitely more discoverable. Personally, I love WMI not only because it provides ready access to reams of documentation about my computer systems, but also because it exposes many methods and writable properties that allow me to quickly and accurately configure many aspects of my systems. Ever since I began working on my <i>Windows PowerShell Step By Step<\/i> book for Microsoft Press, I have written hundreds of WMI scripts and helper functions. This week, I am going to collect together many of the helper functions into a single module that will make access to WMI information easier.<\/p>\n<p>The first function I add to my WMI module is the <b>Get-WMIClassesWithQualifiers<\/b> function that I wrote for the <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2011\/10\/22\/use-a-powershell-function-to-find-specific-wmi-classes.aspx\">Use a PowerShell Function to Find Specific WMI Classes<\/a><i> <\/i>post last Saturday. See that article for details on the function.<\/p>\n<p>Creating a Windows PowerShell module is really easy. I open the Windows PowerShell ISE and paste my <b>Get-WMIClassesWithQualifiers<\/b> function into the script pane. This technique is shown in the following figure.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2766.hsg-10-24-11-1.png\"><img decoding=\"async\" title=\"Image of technique to create module\" alt=\"Image of technique to create module\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2766.hsg-10-24-11-1.png\" \/><\/a><\/p>\n<p>After I have pasted the first function into the module, I need to save the module so that I do not lose my work. I save the module into my scratch directory (called FSO off the root drive), giving me a place to work. After I have completed the module, I will use my <b>Copy-Modules<\/b> function to install the module into my user module location. The key thing to remember when saving a module is that the file extension must be <b>.psm1<\/b><i>. <\/i>By default, the Windows PowerShell ISE saves files with a <b>.ps1<\/b><i> <\/i>extension, which is a Windows PowerShell script. A <b>.psm1<\/b><i> <\/i>file extension indicates a Windows PowerShell module. This technique is shown in the following figure.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/3527.hsg-10-24-11-2.png\"><img decoding=\"async\" title=\"Image of saving module with .psm1 extension\" alt=\"Image of saving module with .psm1 extension\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/3527.hsg-10-24-11-2.png\" \/><\/a><\/p>\n<p>I decided that I would also like to include the <b>Get-WmiClassMethods<\/b> function and the <b>Get-WMIClassProperties<\/b> functions in my WMI module. The great thing about these two functions is that they make it really easy to find methods and properties that are implemented and writable. This is really important! Because of the way that the <b>Get-Member<\/b> cmdlet works, it shows everything in WMI as read\/write. And though this is technically correct (at least to a point in that I can update an object in memory), it is of very little practical value because I cannot update all WMI objects. For example, if I use the <b>Get-Member<\/b> cmdlet on the <b>Win32_LogicalDisk<\/b> WMI class, it reports that all of the properties are <b>Get<\/b>\/<b>Set<\/b>. Obviously, I cannot use WMI to change the size of my disk drive, and I am not certain I want to attempt to change the amount of free space on my drive by using WMI. This is shown in the following figure.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/6888.hsg-10-24-11-3.png\"><img decoding=\"async\" title=\"Image of running Get-Member on Win32_LogicalDisk WMI class\" alt=\"Image of running Get-Member on Win32_LogicalDisk WMI class\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/6888.hsg-10-24-11-3.png\" \/><\/a><\/p>\n<p>Therefore, I need a better methodology for obtaining this information. I can use the Windows Management Instrumentation Tester (WbemTest) tool, but unfortunately, it requires me to examine every property in an individual manner to discover if a property is writable. This is not an acceptable solution when attempting to do a bit of quick scripting work.<\/p>\n<p>Back in March of 2011, I wrote a <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2011\/03\/12\/explore-wmi-methods-and-properties-via-powershell-script.aspx\">series of Hey, Scripting Guy! posts<\/a> where I explored writable WMI properties and implemented WMI methods. I decided to adapt those scripts to meet the need of my <b>HSGWMIModule<\/b>. The original script from March 12, 2011, is shown on the <a href=\"http:\/\/gallery.technet.microsoft.com\/scriptcenter\/10670e14-4cf1-4ce5-99d0-fc4ca80dac2c\">Scripting Guys Script Repository<\/a>. I uploaded the <a href=\"http:\/\/gallery.technet.microsoft.com\/scriptcenter\/Get-Writable-WMI-82122652\">modified functions<\/a> to the Scripting Guys Script Repository so that you will have them if you wish to follow along.<\/p>\n<p>I made the following changes to the original functions:<\/p>\n<ol>\n<li>It no longer checks for all WMI classes in a particular namespace. Instead, I have limited the scope to a single class.<\/li>\n<li>I made the <b>$class<\/b> parameter a mandatory parameter. You must supply a WMI class name to use the modules.<\/li>\n<li>I added comment-based help, and included examples of use.<\/li>\n<li>I changed the default computer name from &#8220;.&#8221; to <b>$env:computername<\/b> so that an actual computer name is used.<\/li>\n<li>I perform a <b>[wmiclass]<\/b> cast of the string supplied to <b>$class<\/b> so that it converts the string into a management object. This precludes the use of the <b>Get-WmiObject<\/b> cmdlet to return management objects.<\/li>\n<li>I removed the &#8220;entry&#8221; to the script, so running the script loads the functions into memory, but does not execute any searches.<\/li>\n<\/ol>\n<p>So I have added the newly revised modules to my <b>HSGWMIModule<\/b>. I import the module from my scratch location, and use the <b>Get-Command<\/b> cmdlet (<b>gcm<\/b> is alias) to see the names of the functions contained in the module. Here are the commands and associated results:<\/p>\n<p style=\"padding-left: 30px\">PS C:\\Users\\edwils&gt; Import-Module C:\\fso\\HSGWMImodule.psm1<\/p>\n<p style=\"padding-left: 30px\">PS C:\\Users\\edwils&gt; gcm -Module hsg* | select name<\/p>\n<p style=\"padding-left: 30px\"><span style=\"text-decoration: underline\">Name<\/span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs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; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/p>\n<p style=\"padding-left: 30px\">New-Underline&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>The complete <a href=\"http:\/\/gallery.technet.microsoft.com\/scriptcenter\/HSGWMIModuleV1-eb30e6bf\">HSGWMIModuleV1 appears on the Scripting Guys Script Repository<\/a>. You should download it, install it, and play with it. It is cool.<\/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\">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>\n<p><b>Ed Wilson, Microsoft Scripting Guy<\/b><\/p>\n<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: In this article, Microsoft Scripting Guy Ed Wilson begins part 1 of a multipart WMI helper function module for Windows PowerShell. &nbsp; Microsoft Scripting Guy Ed Wilson here. While I was teaching my Windows PowerShell Best Practices class in Montreal, we spent an entire day talking about Windows Management Instrumentation (WMI). While WMI seems [&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-12301","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: In this article, Microsoft Scripting Guy Ed Wilson begins part 1 of a multipart WMI helper function module for Windows PowerShell. &nbsp; Microsoft Scripting Guy Ed Wilson here. While I was teaching my Windows PowerShell Best Practices class in Montreal, we spent an entire day talking about Windows Management Instrumentation (WMI). While WMI seems [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/12301","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=12301"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/12301\/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=12301"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=12301"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=12301"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}