Weekend Scripter: Easily Find Schema of a WMI Class Via PowerShell

Doctor Scripto

Summary: Microsoft Scripting Guy, Ed Wilson, shows how to easily find the schema of a WMI class by using Windows PowerShell.

Microsoft Scripting Guy, Ed Wilson, is here. Well it is exactly two months until the Windows PowerShell Saturday in Charlotte, North Carolina in the United States. You should definitely make plans to attend. Our available speaker slots are pretty much filled. Cool! We will offer three tracks, and we did not even need to call for speakers. I LOVE the Windows PowerShell community!

We will have a Beginner track, an Applied track (Windows PowerShell with Active Directory, SQL Server, SharePoint, Exchange, and so on), and an Advanced track. On the Advanced track, we will be doing the first ever Iron Scripter competition. This competition is like the Iron Chief, but with cmdlets instead of carbs. Can an advanced scripter really take only four Windows PowerShell cmdlets and create a tasty meal? Check it out. It should be educational and downright fun as well.

Registration is not yet open, but when it does, the 200 tickets will go quickly. We sold out the first Windows PowerShell Saturday in Columbus Ohio in 13 days. I anticipate this one will sell out even sooner. For many attendees, this will be their first chance to see some of the amazing stuff that you can do with Windows PowerShell 3.0 and Windows Server 2012.

Finding the schema of a WMI class the easy way

Anyone who has read the Hey, Scripting Guy! Blog for very long knows that when I talk about WMI, I do not write very much before I show you the Windows Management Instrumentation Tester (WBEMTest). One of the main things I look at in the WBEMTest is the schema of the WMI class that I am investigating. For example, the schema for Win32_CurrentTime (an abstract class) is shown in the image that follows.

Image of command output

Click, click, click is not my idea of the easy way to retrieve the Managed Object Format (MOF) for a WMI class. It is easier to use Windows PowerShell to do this. In fact, I can do this in the following two lines of Windows PowerShell code:

$mof = ([wmiclass]”Win32_currenttime”).gettext(“mof”) -replace “\;”,”`r”

$mof | clip ; notepad

There are a couple of things that are pretty cool about this little bit of code. The first is using the [wmiclass] type accelerator to obtain a reference to a specific WMI class. The [wmiclass] type accelerator returns a ManagementClass Class .NET Framework object. The ManagementClass class is very powerful, and it contains numerous methods and properties. The one I am interested in today is the GetText Method. The GetText method accepts a single value—the format of the textual representation to create. This is easy, because there is only one accepted value: a MOF representation. Therefore, our code so far looks like the following:

ManagementClass accelerator

WMI class name

GetText method

Format

[wmiclass]

Win32_CurrentTime

GetText

MOF

When I run the command, things are a bit jumbled. The jumbled output is shown in the image that follows.

Image of command output

To clean up the output a bit, I need to do a few things to the text. I replace semicolons with a carriage return, and the output cleans up nicely as shown here:

 -replace “\;”,”`r”

So I have the output I want, and I store it in a variable. This line of code is shown here:

$mof = ([wmiclass]”Win32_currenttime”).gettext(“mof”) -replace “\;”,”`r”

The next code uses no real Windows PowerShell commands, but it is Windows PowerShell code. I take the contents from the $mof variable and pipe them to the clipboard by using the clip.exe utility that is built into Windows. Next, I use the semicolon to create a new logical line, and I call notepad to allow me to easily paste the MOF syntax for the Win32_CurrentTime WMI class into a fresh Notepad for easier examination. This line of code is shown here:

$mof | clip ; notepad

When I type and run the commands, nothing appears in the Windows PowerShell console. This can be seen in the image that follows.

Image of command output

But the MOF of the WMI class is actually on the clipboard, and a new instance of Notepad appears. Therefore, all I need to do is use the CTRL + V key combination to paste it to Notepad. The resulting Notepad is shown here.

Image of command output

Well, this is enough fun for today. If you want to play around, explore using the SendKeys method from the Wscript.Shell COM object to automate this last step. Like I said, it would be just playing, because there are better ways to write to a file. For my needs, I like having the MOF on the clipboard.

Join me tomorrow as we begin a new week about the Script Center.

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy

0 comments

Discussion is closed.

Feedback usabilla icon