This blog post is part of a web series of 5 posts that covers everything that needs to be known about powershell classes. The series covers the creation of a “computer” class that is ‘enhanced’ throughout the series. In the end, we will have a class system that will be able to provision servers / clients (computer objects) in our active directory dynamically respecting specific constraints. In this specefic post, we will see everything you need to know about powershell static methods.

Quick reference:

part 1, covers all the basic concepts of classes in general are covered. We explain the vocabulary, and why we even would like to work with Classes. We learn how to create our first class, and what properties, methods, and constructors are, and how to create them.

In part 2, we tackle powershell Enums. We will expand our base class “Computer” and integrate the use of Enums. Simple, but powerfull.

In part 3 (This post) we discuss powershell static Methods, make our original class more and more autonomous.

In part 4 we tackle a fundamental part of powershell classes (if not the most important one): Powershell class inheritance.

In this blog posts we will go through powershell static methods, and answer questions such as; why use powershell static methods in your classes? Or how to use powershell static methods in our classes?

To answer these questions, we will add additional logic in order to automagically provision a new server in our ad, and generating the server name with the next computer name available respecting a specific naming convention. This will be done by implementing a powershell static method.

Recap from part 2:

At the end of part 2 of this blog post of this series about PowerShell classes where we talked about powershell enums, we had the following code that allowed us to provision a computer object with a specific owner in our active directory, and based on the type of server, we would put the server in a specific OU:

How to create a powershell static method using powershell classes?

As menetionned earlier, we are planning to add a static method that will help us to generate a computer name based on the type of server we want to provision. According to the type of server, the prefix of the server name will be different.

These will be the prefixes of the servers that we will implement in our environment, and use as an example to show case the use of powershell static methods:

  • “HyperV” -> “HYPE-“
  • “Exchange” -> “EXCH-“
  • “ConfigMgr” -> “CONF-“
  • Any other server -> “SERV-“

Each time we add a new server, we will simply use the prefix, and add a 3 digits number behind it. For each new server we simply increment the sufix by one, until we find the next number available.

example: CONF-002 would be the second installed configmgr server.

The logic to get the the results from what we have defined above, can be resumed in the following code:

These few lines of code will return us the new computer name according to the value of $type (which must be of type [ServerType]).

Also, you could very easily, convert this code into a reusable function, by wrapping it within the function {} keyword

Now that we have our main code, we will add this to our class as a new method.

We don’t need much to transform this into a powershell static method and integrate that powershell static method into our main powershell class.

The rules to follow are simple:

If we apply this rule to our use case, we will have the following:

now that we have covered the basics, we can go and create our static function

For the ones in doubt, a powershell static method can only be located in a class. Don’t try to use it as a function, it will not work.

What we want to do, is to use this logic in our constructor, to get the next available computer name immediately.

A static method allows us to call a method without having to create an instance of that class. The only ‘extra’ thing that we need to do, in order to transform a regular method into a static method is to specify the keywordstatic‘ in front of the method, and to NEVER make a reference to the $this variable in that specific static method.

If you ask me, the power of classes resides in their constructors. You can add extra logic that will be evaluated during the instantiation of your object. Adding a few checks in the constructor, will allow you to guarantee that the object cannot be created if the constructors doesn’t valid the parameters within it’s inner logic. You can make your class self sufficient with powershell static methods, since these ones can be called in your constructor, and can help to validate some prerequisites before your instance is created. I’ll show case that in a few lines.

So, having explained that, and having written our new static method, we can add it to our existing class. It will end up looking like this.

Our static method has been added on line 75. The logic inside the constructor has also been added on line 55 (the call to the static method from within the powershell class constructor).

How to create a powershell static method?

But why did we actually create a powershell static method instead of a simple method?

The answer is very simple. In order to use regular methods from powershell classes (or really, any method from any class in any language), we need to have an existing instance first, and call that method from that already existing instance.

Using a powershell static method, allows us to use the logic that is contained in the method without having to create an instance prior to use it. This is exactly what we wanted! Using a powershell static method, we can now use it to call our powershell static method in our constructor, and generate a new computer name, using the nothing else then our class, without even having in instance of it yet.

The powershell static method is called on line 55 like this

It used the same way of calling a static method in dot net -> the name of the class, followed by two double points, and then the name of the method followed by closing parenthesis. (Parameters are not mandatory)

In the end, our complete computer powershell class looks like this:

We can then create several instances of our object one after each other using the same constructor. Each time it will get the latest server existing,  increment the 3 digit number by 1, create the server name, and provision the computer object into the AD.

powershell static method instance class creation

This wraps up part 3 “powershell static methods” of our powershell Class series. Stay tuned for part 4, where we will be talking about powershell class inheritance.

By | 2016-11-27T21:12:00+00:00 November 14th, 2016|Classes, PowerShell Classes, Series|1 Comment

About the Author:

Stéphane is a dynamic and passionate Cloud and datacenter Microsoft MVP since. He is the founder of the Basel PowerShell user Group (BPUG), the co-founder of the French Speaking PowerShell UserGroup (FRPSUG), author, blogger, and received the community award "PowerShell Hero" from Stéphane has implemented microsoft infrastructure solutions in various countries of Europe and is currently working in Basel / Switzerland. Stéphane help his clients to reduce their global infrastructure costs by implementing Microsft infrastructure solutions by combining great products such as System Center, Windows Server, with heavy automation using Windows PowerShell. Stéphane loves languages, Belgium beer, French cheese and French Wine. If any of these topics are of your interest, don't hesitate to come and say hi.

One Comment

  1. […] If you want to dig more into static methods or if you want to see another example, I recommend you read this article. […]

Leave a Reply

%d bloggers like this: