- Install VMware ESXi on Dell PowerEdge - Tue, Oct 25 2022
- Create an Ansible inventory file with psansible.inventory and an Ansible inventory script in PowerShell - Thu, May 26 2022
- PowerShell Classes – Part 4: Constructors - Tue, Dec 27 2016
In a way, classes are the bridge between IT administration and software development. However, classes can also be useful for IT pros who want to improve their scripting skills.
Note that this series targets the PowerShell beginner. Thus, I won't go into details of advanced concepts such as abstraction, inheritance, polymorphism and encapsulation here.
What is a class?
A class is a blueprint for an object. It is used as a model to define the structure of objects. An object contains data that we access through properties and that we can work on using methods.
PowerShell has been all about objects since the beginning. Even without knowing it, you have been working with objects since you used your first cmdlet. If you are not familiar with the concept of an object, have a look at the following example:
If you save the output of the Get-Date cmdlet in a variable $now, you have access to the current date – Tuesday,December the 6th, 8:53:06 PM in this particular case. Our variable $now doesn't just contain the current date and time, but comes with many additional useful features.
For example, we have access to the exact minute using $now.Minute (which returns 53) or to the day of the week using $now.DayOfWeek (which returns Tuesday). I have identified both of these in the screenshot in green. .Minute and .DayOfWeek (the dots are important) are what we call "properties." Properties store information in the object.
We can also work with our object. In the case above, I add 2 days to our existing value residing in $now. The result shows us that adding 2 days to the 6th of December gives the 8th of December. This is what is called a method.
Instantiating an object
When we create a new object, we actually create a new instance of an object. The action of creating the new instance is called "instantiating" the object. It creates a new object in memory based on a particular class.
The example below demonstrates how you can create your own object derived from the PSObject class that comes with PowerShell. This was the method used for creating custom objects prior to PowerShell version 5.
$Properties = @{ "FirstName" = 'Stéphane' "LastName" = 'van Gulick' "WebSite" = 'www.powershelldistrict.com' } $Object = New-object -TypeName psobject -Property $properties $Object
This creates an object that we saved in the $object variable. If we look in our variable, it contains the data in a structured manner, and will be displayed like this in the console:
PowerShell 5.0 added the ability to create your own classes. In the following line, we will create the same object using our own class.
Class Author { $FirstName = 'Stéphane' $LastName = 'van Gulick' $WebSite = 'www.powershelldistrict.Com' } New-Object Author
Note that were we are setting default values for our properties here. PowerShell would then display the object like this:
You can instantiate a class in two different ways:
#Instantiating a class method n°1 [Author]::new() #Instantiating a class method n°2 New-Object Author
Both ways will give us the same result. Method 1 is the same syntax as in C#. This was the only way of creating objects in the first beta versions of PowerShell 5.0.
We can easily add another author, using the same portion of code, simply by changing the property values. We can also create a class without setting default parameters. The class below has four properties – FirstName, LastName, UserName, EmployeeType – each of type String, that have no predefined values:
Class Employee { [string]$FirstName [string]$LastName [string]$UserName [string]$EmployeeType }
Of course I could also have used other data types such as integers, decimals, or Booleans. Notice also that there are no commas after each individual property (as opposed to in a regular param block in a function, for instance).
Subscribe to 4sysops newsletter!
This wraps up our introduction to classes. I will see you in the next post of this series about classes where I will be covering the basics of properties.