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

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

  • Comments 7
  • Likes

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

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • <p># more weekend fun.</p> <p>$mof=[xml][string]$x.psbase.GetTExt(&#39;wmidtd20&#39;)</p> <p>$mof.Instance.property|select name</p>

  • <p>Oops! - Sorry I pasted the wrong one:</p> <p>$mof=[xml][string]([wmiclass]&#39;Win32_BIOS&#39;).GetText(&#39;wmidtd20&#39;)</p> <p>$mof.SelectNodes(&#39;//PROPERTY&#39;)</p> <p>As a bonus here is a way to get instance data.</p> <p>([xml][string]([wmi]&#39;\\.\root\cimv2:Win32_LocalTime=@&#39;).GetText(&#39;WmiDtd20&#39;)).SelectNodes(&#39;//PROPERTY&#39;)</p> <p>Usefulness: Use a transform to get very detailed and colorful reports from the XML. &nbsp;XML data can be directly loaded in SQL databases as queryable XML fields.</p>

  • <p>@JRV this is great. I love it. Thank you for sharing.</p>

  • <p>@ed - it&#39;s cool stuff. &nbsp;The XML output allows us to get really crazy.</p> <p>I have played with this a bit. &nbsp;At times when I am at a loss as to how to extract things I have found this useful although that was in VBScript. Since PowerSHell I have not had to do that.</p> <p>PosH 3 will have more support for CIM and fo XML so many more things should be &nbsp;possible. I can&#39;t wait to play.</p>

  • <p>Hi Ed,</p> <p>this is something new to me.</p> <p>A very interesting way to see the class definition.</p> <p>Thanks jrv! This is awesome!</p> <p>Klaus.</p>

  • <p>@K_Schulte Yes, I love using this to look at the schema. I am glad you liked the article. </p>

  • <p>@K_Shulte</p> <p>Thanks - glad you liked it. I am really just trying to egg Ed on to do more excellent blogs. &nbsp;He seems to frequently get at things that I have not been able to uncover. &nbsp;I think he has some resources at MS and int the tech-bloggers community. </p> <p>I am sure that soon Ed will show us how to create an dimplement a functionaly calls using POwerShell. &nbsp;Thisis something that usually requires a binary compiled in Visual Studio or other tool to do. &nbsp;PowerShell can emit a DLL so it can do this. Admins can finally create WMI extension classes to summerize data and to instrument older programs.</p> <p>I think PowerShell is awesome. In fact it is so awsome that we might need to banish Admins from using it and require that only specially trained techs with blue propeller hats can use PowerShell. &nbsp;After all, if Admins can do all of this, who needs engineers? - MIT eat your heart out!</p> <p>¯\_(ツ)_/¯</p>