{"id":2104,"date":"2014-01-30T00:01:00","date_gmt":"2014-01-30T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2014\/01\/30\/invoking-cim-methods-with-powershell\/"},"modified":"2022-06-20T14:49:25","modified_gmt":"2022-06-20T21:49:25","slug":"invoking-cim-methods-with-powershell","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/invoking-cim-methods-with-powershell\/","title":{"rendered":"Invoking CIM Methods with PowerShell"},"content":{"rendered":"<p><strong>Summary<\/strong>: Guest blogger, Trevor Sullivan, talks about invoking CIM methods via Windows PowerShell.<\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. Today we have another guest post from Trevor Sullivan. Trevor is an <a href=\"https:\/\/devblogs.microsoft.com\/scripting\/honorary-scripting-guys-announced\/\" target=\"_blank\" rel=\"noopener\">Honorary Scripting Guy<\/a>, and a recognized Microsoft Community Contributor (MCC). To see more of Trevor\u2019s guest posts, see these <a href=\"\/b\/heyscriptingguy\/archive\/tags\/windows+powershell\/guest+blogger\/trevor+sullivan\/\" target=\"_blank\" rel=\"noopener\">Hey, Scripting Guy! Blog posts<\/a>.<\/p>\n<p><strong>\u00a0 \u00a0 \u00a0Note<\/strong>\u00a0 This is the fourth in a series of five posts by Trevor where he talks specifically about using the CIM cmdlets.<br \/>\n\u00a0 \u00a0 \u00a0To catch up, read the following posts:<\/p>\n<ul>\n<li><a href=\"https:\/\/devblogs.microsoft.com\/scripting\/what-is-cim-and-why-should-i-use-it-in-powershell\/\" target=\"_blank\" rel=\"noopener\">What is CIM and Why Should I Use It in PowerShell?<\/a><\/li>\n<li><a href=\"https:\/\/devblogs.microsoft.com\/scripting\/introduction-to-the-cimcmdlets-powershell-module\/\" target=\"_blank\" rel=\"noopener\">Introduction to the CIMCmdlets PowerShell Module<\/a><\/li>\n<li><a href=\"https:\/\/devblogs.microsoft.com\/scripting\/use-powershell-to-manipulate-information-with-cim\/\" target=\"_blank\" rel=\"noopener\">Use PowerShell to Manipulate Information with CIM<\/a><\/li>\n<\/ul>\n<p>Here\u2019s Trevor\u2026<\/p>\n<p>In yesterday\u2019s post, we talked about reading and writing CIM properties. Today, we\u2019ll look at WMI\/CIM methods.<\/p>\n<p>In the context of programming terminology, methods generally represent actions that you can perform against an object. In CIM\/WMI, this is no exception. Similar to properties, methods can be declared as static or instance methods. Static methods are declared on a class, and generally don\u2019t operate on any particular instance of that class. Instance methods are generally actions that are performed against a specific instance of the class.<\/p>\n<p>Classes are blueprints of an object that do not represent an instance of the object itself. For example, think of a car blueprint. The blueprint represents what a car might look like, how many wheels it has, how many doors it has, the size of the engine, what type of transmission it has, and so on. Blueprints are not actual cars, however. Therefore, a static method might be defined at the class (blueprint) level called <strong>Build()<\/strong>, which produces a car.<\/p>\n<p>Instance methods generally operate on an instance of the class that declares the method. To get an instance of a car, you must first call the blueprint\u2019s <strong>Build()<\/strong> method. When you have a specific car (instance) that is built according to the blueprint\u2019s (class) specifications, you could begin calling instance-level methods on it.<\/p>\n<p>On any given car instance, you might have a method called <strong>Drive()<\/strong>, which causes the car to move. Similarly, you might have an **OpenHood() **method, which opens the hood of the vehicle. Because these methods operate on a specific car, they are better declared at the instance (specific car) level as opposed to the class (blueprint) level.<\/p>\n<p>Moving from the car example to a software example, you could have a class called <strong>Process<\/strong>. This class does not represent a specific process (such as svchost.exe or iexplore.exe), but rather, it defines a blueprint for what a process looks like.<\/p>\n<p>In CIM\/WMI, there is in fact a class called <strong>Win32_Process<\/strong> that exists in the <strong>root\\cimv2<\/strong> WMI namespace. This class has a static (blueprint) method called <strong>Create()<\/strong>, which allows you to create (start) a process in a Windows operating system. After you have a process, you have the ability to terminate the process using the <strong>Terminate()<\/strong> method. The <strong>Terminate()<\/strong> method is an instance-level method because the action of terminating is performed against a specific process.<\/p>\n<h2>Calling an instance method<\/h2>\n<p>Here is some sample script that shows how to call the <strong>Win32_Process.Terminate()<\/strong> CIM method by using the new <strong>Invoke-CimMethod<\/strong> cmdlet. This method call is particularly easy because we do not have to specify any method parameters.<\/p>\n<p style=\"margin-left:30px\">\n  # Start a new instance of notepad.exe\n<\/p>\n<p style=\"margin-left:30px\">\n  notepad;\n<\/p>\n<p style=\"margin-left:30px\">\n  # Retrieve the Win32_Process instance for notepad.exe\n<\/p>\n<p style=\"margin-left:30px\">\n  $Notepad = Get-CimInstance -ClassName Win32_Process -Filter &#8220;Name = &#8216;notepad.exe'&#8221;;\n<\/p>\n<p style=\"margin-left:30px\">\n  # Invoke the Win32_Process.Terminate() method\n<\/p>\n<p style=\"margin-left:30px\">\n  Invoke-CimMethod -InputObject $Notepad -MethodName Terminate;\n<\/p>\n<h2>Calling a static method<\/h2>\n<p>Static methods can be called easily also. Let\u2019s take a look at the <strong>Win32_Process.Create()<\/strong> method, which creates a new process. The <strong>Create()<\/strong> method is different from <strong>Terminate()<\/strong> because it requires that we pass method parameters to it, specifically the path to the process that we want to start. How do we know what those parameters are?<\/p>\n<p>Let\u2019s first look at a list of CIM methods that are available for the <strong>Win32_Process<\/strong> class:<\/p>\n<p style=\"margin-left:30px\">\n  (Get-CimClass -ClassName Win32_Process).CimClassMethods;\n<\/p>\n<p>Then, let\u2019s narrow things down specifically to the <strong>Create()<\/strong> method:<\/p>\n<p style=\"margin-left:30px\">\n  (Get-CimClass -ClassName Win32_Process).CimClassMethods[&#8216;Create&#8217;].Parameters;\n<\/p>\n<p>Now we can see a list of all the method parameters that <strong>Create()<\/strong> requires:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-1-30-14-1.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-1-30-14-1.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>To use <strong>Invoke-CimMethod<\/strong> to call the <strong>Create()<\/strong> method, we need to create a <strong>HashTable<\/strong> of parameters to pass into it.<\/p>\n<p style=\"margin-left:30px\">\n  $Arguments = @{\n<\/p>\n<p style=\"margin-left:30px\">\n  \u00a0\u00a0\u00a0 CommandLine = &#8216;notepad.exe&#8217;;\n<\/p>\n<p style=\"margin-left:30px\">\n  \u00a0\u00a0\u00a0 CurrentDirectory = $null;\n<\/p>\n<p style=\"margin-left:30px\">\n  \u00a0\u00a0\u00a0 ProcessStartupInformation = $null;\n<\/p>\n<p style=\"margin-left:30px\">\n  };\n<\/p>\n<p>Then, we can call the method.<\/p>\n<p style=\"margin-left:30px\">\n  Invoke-CimMethod -ClassName Win32_Process -MethodName Create -Arguments $Arguments;\n<\/p>\n<p>When you run the previous script on a local computer, you should see notepad.exe running. You might wonder why we left out the <strong>ProcessId<\/strong> parameter when we started the process. The reason is that the process ID is an \u201cout\u201d parameter, which is returned to us when the process is launched. We do not need to pass a <strong>ProcessId<\/strong> into the <strong>Create()<\/strong> method, because that simply doesn\u2019t make sense.<\/p>\n<h2>Limitations<\/h2>\n<p>A couple of days ago, I briefly mentioned how WMI methods are not bound to the .NET objects, when you use the CIMCmdlets PowerShell module. Let\u2019s take a look at how things have changed from the traditional WMI days.<\/p>\n<p>One relatively common task that is performed through WMI is terminating processes, which we explored earlier in this post. If you want to terminate a process by using WMI, you could simply call the appropriate WMI method on the WMI object that you received from the <strong>Get-WmiObject<\/strong> cmdlet:<\/p>\n<p style=\"margin-left:30px\">\n  (Get-WmiObject -Class Win32_Process -Filter &#8220;Name = &#8216;notepad.exe'&#8221;).Terminate();\n<\/p>\n<p>If you want to call a static method on a class, such as the <strong>Create()<\/strong> method on the <strong>Win32_Process<\/strong> class, you could use the <strong>[wmiclass]<\/strong> type accelerator to get a reference to the class, and then call the static method by using the dot\/period operator:<\/p>\n<p style=\"margin-left:30px\">\n  ([wmiclass]&#8221;root\\cimv2:Win32_Process&#8221;).Create(&#8216;notepad.exe&#8217;);\n<\/p>\n<p>If you called the method without any parameters and sans the parentheses, Windows PowerShell used to show you the WMI method signature. Unfortunately, you now have to use a bit more effort to figure out the method signature by using an external GUI tool, or by using the <strong>Get-CimClass<\/strong> cmdlet, as previously demonstrated.<\/p>\n<p>Well, that\u2019s all for today! We\u2019ll wrap things up tomorrow by looking at CIM sessions and Windows PowerShell sessions.<\/p>\n<p>~Trevor<\/p>\n<p>That is all there is to invoking CIM methods. CIM Week will continue tomorrow when Trevor will talk about CIM sessions and PS Remoting.<\/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\" target=\"_blank\" rel=\"noopener\">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><strong>Ed Wilson, Microsoft Scripting Guy<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Guest blogger, Trevor Sullivan, talks about invoking CIM methods via Windows PowerShell. Microsoft Scripting Guy, Ed Wilson, is here. Today we have another guest post from Trevor Sullivan. Trevor is an Honorary Scripting Guy, and a recognized Microsoft Community Contributor (MCC). To see more of Trevor\u2019s guest posts, see these Hey, Scripting Guy! Blog [&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":[385,51,56,3,4,211,45,6],"class_list":["post-2104","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-cim","tag-getting-started","tag-guest-blogger","tag-scripting-guy","tag-scripting-techniques","tag-trevor-sullivan","tag-windows-powershell","tag-wmi"],"acf":[],"blog_post_summary":"<p>Summary: Guest blogger, Trevor Sullivan, talks about invoking CIM methods via Windows PowerShell. Microsoft Scripting Guy, Ed Wilson, is here. Today we have another guest post from Trevor Sullivan. Trevor is an Honorary Scripting Guy, and a recognized Microsoft Community Contributor (MCC). To see more of Trevor\u2019s guest posts, see these Hey, Scripting Guy! Blog [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/2104","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=2104"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/2104\/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=2104"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=2104"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=2104"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}