PowerShell Classes Part 3 -- Using Methods
PowerShell version 5.0 introduced the concept of classes to enhance the appeal of PowerShell to the developer community. Traditionally, PowerShell has been a scripting language used by IT operations professionals to automate manual tasks. While in previous versions there have been other methods of constructing classes in PowerShell using C#, none have made it as easy as the changes in version 5.0.
Recap of Classes in General — The Rock Class
Previously. I showed you how to define a class in PowerShell using properties and talked about using enums to define custom types for properties. Continuing with the example, I have defined a Rock class. The Rock class has several properties and some of those properties are using custom “enum” types to limit the possible values for the property. To encourage reuse, the enum types are defined outside the class, as they can be used in any other class or function through the duration of the PowerShell session.
Instantiate the RockNo, not “The Rock”. First, I am going to instantiate or create an instance of a rock using the Rock class definition above. Then, I will assign some properties to the rock.
Rock Class MethodsNext, I am going to define some methods for the class. A method is an action that is performed on an object. I asked myself, “What can I do to a rock? Or what can I do with a rock?” And the answers that came to mind are “ThrowRock” and “SmashRock”. I am going to define methods that accomplish these two actions. I am also going to define a method called “ShowRockLocation”. I will use the Location property, which is an integer value, to graphically show how “far” the rock is away from a fixed point. (In this case, it is the cursor location on the screen.) When I call the “ThrowRock” method, I will be able to visually show how far I threw the rock.
Defining the MethodA class method is just as easy to define as a PowerShell function. However, there is one big difference. I need figure out if the method is going to return a value and specify the returned value’s type on the method definition. If a method is not going to return a value, the return value type is [void]. Starting out with the “ShowRockLocation” method, the method definition looks like this:
From there I am simply using PowerShell to add the code that will graphically show me a location:
Notice that in the “for” loop, I am using the variable $This. When I call the ShowRockLocation method, I will be calling it for the instance of rock that I created earlier ($Rock). $This is a special variable that means, “This instance of the Rock class”. To show the location, I am going to add white space (Filler) for each unit of the location property of the rock, then graphically show the rock with an asterisk.
Invoking the ShowRockLocation MethodTo invoke the ShowRockLocation method, I take my instance of Rock ($Rock) and call the ShowRockLocation method as shown below. I see the asterisk depicting the location of the rock.
Creating and Invoking a Method with an ArgumentNext, I would like to create the ThrowRock method. ThrowRock is going to take an argument, such as the distance in which to throw the rock, using an integer type. The variable $arg represents the distance.
After defining the method, I invoke ThrowRock using the same call as ShowRockLocation, but I provide the argument for the distance. Next, I call ShowRockLocation to see that the location has changed, and indeed, the asterisk has moved, presumably 28 spaces.
Method and EnumsLastly, I am going to create the SmashRock method. Looking back at the class definition, I used an enum definition for the Size property, specifically so I could limit the values of Size to the sizes I want for this method. If the SmashRock method is called upon an instance of a rock that is Large, the method will change it to Medium. If I call SmashRock on a Medium rock, it will change to Small. If I call SmashRock on a Small rock, I will simply write a message to the screen saying the rock is already small.
One Last Word on the EnumFinally, notice that I would not have been able to write the SmashRock method without using either an Enum or a ValidateSet. In that case, there would be no way to account for the infinite variations of a string value for Size. If I had chosen to use a string and then decided to write this method, I could always go back and redefine the Size property to use an enum instead of a string value.
Method RecapTo conclude, methods perform an action against an instance of a class object. It accepts one or more arguments and can return a value as output. Methods perform an action against the current instance of the object using the $This variable. Finally, it is similar to the PowerShell function, in that a function takes certain input (parameters) and can return output but a function does not require an instance of a class nor an object to be called.
More in PowerShell
Use PowerShell Functions to Quickly Simplify Your Scripts
Apr 17, 2023 | Bill Kindle
Microsoft Updates Teams PowerShell Module to Version 5.0
Mar 6, 2023 | Rabia Noureen
How to Create Conditional Access Policies using PowerShell
Jan 4, 2023 | Liam Cleary
Microsoft Releases PowerShell Crescendo 1.1.0-preview01
Dec 8, 2022 | Rabia Noureen
How To Set Environment Variables With PowerShell
Dec 5, 2022 | Sukesh Mudrakola
How to Use a PowerShell Foreach Loop
Oct 24, 2022 | Ivan Mirchev
Most popular on petri