Learn about Windows PowerShell
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.
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.
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:
WMI class name
When I run the command, things are a bit jumbled. The jumbled output is shown in the image that follows.
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:
So I have the output I want, and I store it in a variable. This line of code is shown here:
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:
When I type and run the commands, nothing appears in the Windows PowerShell console. This can be seen in the image that follows.
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.
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 firstname.lastname@example.org, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.
Ed Wilson, Microsoft Scripting Guy
<p># more weekend fun.</p>
<p>Oops! - Sorry I pasted the wrong one:</p>
<p>As a bonus here is a way to get instance data.</p>
<p>Usefulness: Use a transform to get very detailed and colorful reports from the XML. 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's cool stuff. The XML output allows us to get really crazy.</p>
<p>I have played with this a bit. 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 possible. I can't wait to play.</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>@K_Schulte Yes, I love using this to look at the schema. I am glad you liked the article. </p>
<p>Thanks - glad you liked it. I am really just trying to egg Ed on to do more excellent blogs. He seems to frequently get at things that I have not been able to uncover. 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. Thisis something that usually requires a binary compiled in Visual Studio or other tool to do. 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. After all, if Admins can do all of this, who needs engineers? - MIT eat your heart out!</p>