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.

Sponsored Content

Passwords Haven’t Disappeared Yet

123456. Qwerty. Iloveyou. No, these are not exercises for people who are brand new to typing. Shockingly, they are among the most common passwords that end users choose in 2021. Research has found that the average business user must manually type out, or copy/paste, the credentials to 154 websites per month. We repeatedly got one question that surprised us: “Why would I ever trust a third party with control of my network?

Instantiate the Rock

No, 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 Methods

Next, 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 Method

A 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 Method

To 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 Argument

Next, 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 Enums

Lastly, 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 Enum

Finally, 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 Recap

To 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.

Related Topics:


Don't have a login but want to join the conversation? Sign up for a Petri Account

Comments (0)

Leave a Reply

13 Email Threat Types to Know About Right Now

As email threats evolve and multiply, keeping track of them all—and staying protected against the many different types—becomes a complex challenge. Today, that requires more than just the traditional email gateway solution that used to be good enough.

In this eBook you will learn:

  • What are the most common and challenging email attacks for organizations?
  • How to defend against sophisticated email threats, such as spoofing, social engineering, and fraud
  • How to protect employees at the inbox level with the right technologies and security-awareness training
  • How to use a multilayered protection strategy to reduce susceptibility to email attacks and better defend your business and employees

Sponsored by: