PowerShell Not Your Father’s Command Line Part 29 of 31: Demystifying MSDN and PowerShell static syntax

Originally, this post was going to be about GUIs, but after waking up this morning and seeing a comment on Part 28, I figured that I’d address that comment in this post.

Paul Bendall writes:

But how does an ITPro without a developer background start to use MSDN and understand terminology such as static, override, []::Something?

Let’s demystify some of the things at MSDN so that maybe you guys without a developer background can make sense of it.

Terminology Elevator – Ground Floor – Objects

Objects are… um… objects in programming terminology. An object could be words, numbers, a computer, anything really. In Appendix B in our book, I talk about objects at a really high level. Let’s take a similar approach here. Let’s talk about a keyboard.
Rapoo Keyboard from Hong Kong
A keyboard is an object. It has keys – which means that a keyboard could have a collection of objects. Yes, collection can be a technical term referring to a grouping of like or related objects. Now let’s look at a keyboard key… what can you tell me about it?

  • What color is the key?
  • What character or characters are on the keys?
  • How big or small is the key?
  • Is the key enabled?

The answer to these questions would be called properties of the keyboard key. They describe the object in terms of adjectives and capabilities.

  • Press the key.
  • Release the key.

These actions show that the key can respond to events. When an event happens, you can trigger a piece of code to run. In some cases, you may be able to have code run before or after an event happens, depending on the object and event.

Now let’s look at the Caps Lock key. When you press it, the alphabetical keys TYPE CAPITAL LETTERS. This means that not only can this key respond to being pressed, but it also can perform an action called a method. Yes, this also means that the object may call a method while responding to an event.

So an object can have properties, methods, and events.

Terminology Elevator – Level 1 – Class, Static, and Constructor

A class is a container that holds the methods, events, and properties of an object. Using our keyboard key example, a class would be the collection of all of the properties, methods, and events that belong to a keyboard key.

If you find yourself using the New-Object cmdlet and getting the “Constructor not found.” error, that basically is telling you that you are working with a static class.

A constructor is a piece of code that states how to create a new object. This is the piece of code that gets run when an object is created with the New-Object cmdlet.

If the constructor isn’t in the class, then the class is considered static. If something is called static, this means that they are not created, destroyed, or changed – they are simply used.

In PowerShell, the :: operator is used to call out to static methods and properties. The syntax is as follows:

[Namespace.Namespace.ClassName]::Method()

or

[Namespace.Namespace.ClassName]::Property

Yesterday, we looked at the System.Windows.Forms.SystemInformation class and some of its properties. There is also a TechNet article on using static classes and methods in PowerShell.

Terminology Elevator – Level 2 – Assemblies and Namespaces

Assemblies and namespaces are ways to organize code. An assembly is a physical way of organizing code – commonly in a DLL file. A namespace is a logical way of organizing code – grouping code for the same object or collection of objects together.

Looking at the top of the SystemInformation class page on MSDN, you can see that this particular class lives in the System.Windows.Forms.dll assembly. It also belongs to many namespaces – collectively, it belongs to System.Windows.Forms. However, Forms is a namespace that belongs to Windows, which is a namespace that belongs to the System namespace.

Russian Matryoshka dolls - pic taken from http://nelietatravellingadventures.blogspot.com/2010/11/matryoshka-or-nesting-dolls-from-russia.html
One thing you’ll notice with the naming scheme is that the namespaces are stacked like Russian matryoshka dolls – the largest namespace is on the outside, and the smaller namespaces are nested inside, all the way down to the class – which is like that tiny doll at the end.

Terminology Elevator – Observation Deck – Overrides

By default, there are actions that a command can do automatically. However, you may want to customize those actions, which can be done by overriding the action. Basically, if you write code that overrides the default behavior, your code supersedes the default code.

Conclusion

A lot of what you’ll see on MSDN is based on object-oriented programming concepts. Hopefully this post gives you a little idea as to what those terms mean and can help you on your way in using static classes and methods in PowerShell.

2 thoughts on “PowerShell Not Your Father’s Command Line Part 29 of 31: Demystifying MSDN and PowerShell static syntax

  1. Many thanks for the explanation, it makes some sense but I think it might take a while to get it through my dumb ITPro head! I’m trying to understand the power of web services (SOAP) at the moment especially SharePoint WSS 3.0 for reporting. I want to get away from dumping into CSV, tidy up in Excel and save to SharePoint.

    Another opportunity came along last week after a Tweet from the Scripting Guys when I asked if the following could be done via Exchange Web Services managed API rather then Outlook Interop, http://blogs.technet.com/b/heyscriptingguy/archive/2011/05/24/use-powershell-to-export-outlook-calendar-information.aspx?utm_source=twitterfeed&utm_medium=twitter.

    Paul

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>