未加星标

A practical use case for PowerShell Classes Part 2: Powershell enum

字体大小 | |
[系统(windows) 所属分类 系统(windows) | 发布者 店小二04 | 时间 2016 | 作者 红领巾 ] 0人收藏点击收藏

A practical use case for PowerShell Classes Part 2: Powershell enum

In part two of this web series about powershell classes, I would like to talk about how to use a powershell enum and go through a use case which will point out their use, and why you really want to integrate them into your scripts.

If you have missed the beginning of this blog post series,See part onehere, and learn how powershell constructors and theiroverloads works in powershell 5 classes.

how to write a powershell enum to use in a powershell class

Since version 5 of powershell, we have thepossibility to create powershell enumerations. Or more commonly called powershell enum .

What is a powershell enum? why would we even want to use a powershell enum? How do we create a powershell enum?

I’ll try to answer all of these questions in this blog post.

If you think something is missing in this article, or would like to see something to be covered more in dept, please let me know in the comment section below or per twitterat @stephanevg

To create a new powershell enumartion,we will use the new keyword “ Enum “.

The very basics for a powershell Enum to exist would be to having something as represented as followed:

Enum MyGreatEnum { }

A powershell enum is used to offer a limited list of elements of a specefic type to your object.

For example, you could have a Enum called “ ServerType ” that would contain a list of all of your different type of servers. These are the ones that I have my lab, and which we will build the example.

Enum ServerType { HyperV Sharepoint DSC Exchange Lync Web ConfigMgr }

When you would like to have a reference to one of you elements from your list you will query it as followed:

[ServerType]::ConfigMgr

This would return the value “ ConfigMgr ”

See by your self in the example below:


A practical use case for PowerShell Classes Part 2: Powershell enum

powershell Enum

The value that is returned, is of type [ ServerType ] , which we can use to include generic validation logic to our powershell codeandby keeping it easily extensible .

I can imagine this to be used like some sort of ‘ externalized parameter set ‘ which can easily be modified, without impacting to drasticallyyour code when we add a new server type to manage. The same powershell enum can then be used in several different places in our code while limiting copy pasting.

If you want to execute an IF condition if you result of some query is equal to one of the values in your list, you would have to either use of bunch of if’s and elseif’s , a Switch, or a foreach construct.

Let’s say, you have a new server type like for example a SCOM server, that would mean that you would have to adapt your if’s or your switch statements. Now if we do or methods like above, we can restrict it on the type and avoid us to adapt our maincode. Using powershell enums will give quite some easy extensibility to our code .

Instead of having something like this:

if ( ($NewServerType -eq ConfigMgr) -or ($NewServerType -eq Lync) -or($NewServerType -eq Sharepoint) ){ #Do Custom actions }

We could do something as simple as this:

if ($NewServerType -is [ServerType]){ #Do custom actions }

The advantage of the second method, is that to extend the functionality of our script to accept a new type of server, the only thing we need to do is to add new entryto the Enum Class.

Using the‘ -is ‘ operator combined with a Enum Type, allows to check if several conditions are true , using very compact syntax .

Powershell enum contains constants:

We can also use a powershell enum to assign a value to each of the properties that composes the powershell enum, but there is an important thingto keep in mind: A powershell Enum can only be a constant .

A powershell enum can only containconstants. This means it cannot be a word, or theresult of some query.

The code below assigns a constant to each value.

enum ServerEnvironment { developpement = 1; Integration = 2; Production = 3 ; } We can later on cast a value to the type of our powershell enum (in my example [ServerEnvironment]) and we will have the corresponding environment back.
A practical use case for PowerShell Classes Part 2: Powershell enum

Constant can be assigned in any order , but it is not an obligation. By default, if you omit to put constants on your enums, they will still be accessible as in the example above. The only thing to keep in mind is that a powershell enum is0 based .

One last thing to add to this, is the fact that you can add constants to your powershell enum, that does not necessarily starts at 0. In other words, you will be able to use your powershell enum as followed as well:

enum FavoriteNumbers { Stephane = 7 JeffreySnover = 5 SnoopDogg = 89 } Using a powershell enum in a concrete example

Ok, now that we have the theory covered around how to create a PowerShell enum, let’s make this concrete with an example. For that, I will reuse the class I wrote inpart 1 of thisblog series, and we will continue on this based through this complete series.

As a reminder, the code we ended up having at part 1 is the following one:

We will simply integrate our S erverType powershellenum into our already existing code (see above), and make our existing classes more robust, and better prepared for the future.

As a reminder, this is our ServerType enum:

If we create an instance of our powershell class without the powershell enum, this is what the current result will be:


A practical use case for PowerShell Classes Part 2: Powershell enum

We do have an object, where the type is set to this string ‘ plop ‘ and the computer object has been created, but it set it in the “ computers ” container ofour Active Directory. Also, you might have noticed that the computer account was named “ woop “, which is definitely not an intelligent name for a computer (It might be for dog though!)

Please leave a comment if your dog’s name is “Woop”!

Using a powershell enum, we can make this more dynamic, and error prone. We will adapt the existing constructor to take a parameter $type of type [ Servertype ] and according to the type that is specified, we will put the computer in a specific organisational unit, and give it a correct prefix. Seeting the computer account in the right OU using powershell classes and enums

Now that we have created our first computer object using powershell classes, we can go a step further and integrate the powershell enums to our logic.

We

本文系统(windows)相关术语:三级网络技术 计算机三级网络技术 网络技术基础 计算机网络技术

主题: PowerShell
分页:12
转载请注明
本文标题:A practical use case for PowerShell Classes Part 2: Powershell enum
本站链接:http://www.codesec.net/view/484601.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 系统(windows) | 评论(0) | 阅读(33)