How to Use PowerShell's Invoke-WMIMethod

The ability to query properties isn't the only thing available by WMI, you can also use it to take action through various WMI providers by invoking methods. In this tutorial, you will be acquainted with PowerShell's Invoke-WMIMethod

Setup

If you have not already done so, click open Windows PowerShell ISE.

Step One

The first thing we will do is make sure those particular windows features are turned off. In this particular tutorial we use W3SVC so in your start menu, type in Windows Features and you will have a small window pop up where you can choose by check box, the features you want on or off. Select web management tools and everything under World Wide Web Services, you may have to scroll down a bit.
Your window should appear like the following image:

Step Two

Methods are code blocks that are designed to perform a special action. You can actually run the following command:

Example


    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <title></title>
    </head>
    <body>
    <form id="form1" runat="server">
    <div>
    </div>
    </form>
    </body>
    </html>
    

Copy and Try it

You use the –path parameter to specify an instance of the Win32_Service class to run the method on. In the above command it tells it to run against the Win32_Service instance where the Name property is equal to W3SVC.

The output is presented as the following:

???

Step Three

At times, methods require arguments to be specified so that it knows what to do. An excellent example is the Create method for the Win32_Process class. This method is used to create a new process, but in order for it to do so, it needs the name of the executable that is to be run.

An example of this command being run is presented below:

Example

protected void Page_Load(object sender, EventArgs e)
  {
   if (!Page.IsPostBack)
   {
    string strMessage = "Welcome to the Page_Load method!";
    Response.Write(strMessage);
   }
  }
   

Copy and Try it

The output is presented within the console as well as with internet explorer launching, as presented below:

Remarks last but not least

There are various applications to today's lesson, just keep in mind that in order to run various commands and scripts in PowerShell, you must have administrative rights. Thank you for being a valued reader. Join us next time for additional Windows PowerShell tutorials! Till then…