Deconstructing the components of a Class
- The many layers of an onion
- Building your first PowerShell Class
- Deconstructing the components of a Class
- Expanding on your first PowerShell class
- Unit Testing PowerShell Classes with Pester
In the introduction to this series, I talked about the work that I am doing to create a PowerShell v5 class based Desired State Configuration (DSC) resource. As I started this project, I quickly turned to the ol’ google-foo to see what others were saying about best practices and tips. The first thing that I realized was that the Occam’s Razor search term Powershell Classes was not going to help me. There was a bit of a facepalm. I encourage you to laugh along with me as you probably suspect the results that I received were not what I was looking to find. Along the way, I found some interesting resources that revealed more pieces of this puzzle. I found myself standing directly in an interesting intersection of ideas.
Before you can fully appreciate the power and benefits of Classes and Methods, it would help to understand this core Object Orientated model. For many of you, the idea of an object in PowerShell is second nature and really just what you expect. If I run a PowerShell cmdlet, I will likely receive a PSObject of some type with output. If I were to run the command Get-Service -Name Winmgmt inside of my Windows machine, I would expect a nicely formatted table with columns (Status, Name, DisplayName are called properties) that I can pipe to a filter or store in a variable. Let’s continue to explore this. If we store the output into a variable, we would be able to drill down into all the things that this object can do. The best way to view the object is with Get-Member cmdlet (see figure below). You will notice that the output has several Types. Today, we will look at two in particular – Property and Method.
Typical PowerShell scripts contain parameters to which we assign values when calling the script or function. For Objects, these are defined as properties. With a property, you will normally see one of two types – Read/Write or ReadOnly. There is a third type that we will discuss in a later post called Hidden. Suffice it to say that those properties are typically used inside of the Class methods themselves and are Private to the Class (more on Public vs Private tomorrow).
If you have ever written a large PowerShell script, you have probably created functions that live inside of that script. We use functions to prevent repeating code in multiple places and to streamline results or actions. In the simplest of terms, a Method equates to a Function. Unlike functions in a script, every action of a Class is a method of some type. In our example of Get-Service -Name Winmgmt, we are telling the PowerShell to create a new object of type [System.ServiceProcess.ServiceController]::new() and then assign the Parameter Name to the Property ServiceName. When this happens, the code will automatically perform a GetServices(ServiceName) call and return the state. If I pass this into a variable then I can execute methods. Below is an example of Finding, Stopping, and Starting a service using the class methods
Today, we dipped our toe into a very powerful aspect of Object-Oriented programming. The goal today is to show that as a PowerShell scripter, you have taken advantage of this all along. As we move through this series, we will be go further into this concept. In our next post, we will get our hands dirty and start exploring building our own PowerShell classes.
Note: While I was writing this post, I ran across another great blog on using Classes. This post also happened to talk about [System.ServiceProcess.ServiceController]. This was complete coincidence but I felt there was some great detail here.
Use PowerShell to Work with the .NET Framework Service Classes