Articles

PowerShell articles, tutorials, and guides from community experts.

Don Jones
PowerShell for Admins

PowerShell v5: Class Support

This post is based on the September 2014 preview release of WMF 5.0. This is pre-release software, so this information may change.
One of the banner new features in PowerShell v5 is support for real live .NET Framework class creation in Windows PowerShell. The WMF 5.0 download’s release notes has some good examples of what classes look  like, but I wanted to briefly set some expectations for the feature, based on my own early experiences.
The primary use case for classes, at this point, is for DSC resources. Rather than creating a special PowerShell module that has specially named functions, live in a specially named folder, and work in a special way - that’s a lot of special, which means a lot of room for error - classes provide a more declarative way of creating DSC resources.
But we’re a bit ahead of ourselves. What’s a class?
In object-oriented programming, a class is a hunk of code that provides a specific interface. Everything in the .NET Framework is a class. When you run Get-Process in PowerShell, for example, you are returning objects of the type System.Diagnostics.Process - or, in other languages, objects of the class System.Diagnostics.Process. Each process is an instance of the class. The class describes all the standardized things that a process can show you (like its name or ID), or that it can do (like terminate). Programmers build the functionality into the class itself.
Classes can have static properties and methods - these are hunks of code that don’t require an actual instance of a process. For example, you can start a process without having a process in the first place. The System.Math class in .NET has lots of static members - the static property Pi, for example, contains the numeric value of pi to a certain number of decimal places. The static Abs() method returns the absolute value of a number.
PowerShell classes are designed to provide similar functionality. The trick with PowerShell classes, at least at this stage of their development, is that they don’t add their type name to any kind of global namespace. That is, let’s say you write a class named My.Cool.Thing, and you save it into a script module named MyCoolThing.psm1. You can’t just go into the shell and run New-Object -TypeName My.Cool.Thing to create an instance of the class, because there’s nothing in PowerShell (yet) that knows to go look for your script module to find the class. That’ll likely change in a future release, but for right now it means classes are kind of limited.
The basic rule is that you can only use a class _within the same module that contains the class. _That is, the class can only be “seen” from within the module. So, your MyCoolThing.psm1 module might define a class, and then might also define several commands (functions) that use the class - that’s legal, and it will work. You still can’t use New-Object; instead, you’d instantiate your class by using something like ClassName::new(), calling the static New() method of the class to instantiate it. I expect New-Object will get “hooked up” at some point, but it might not be until some future version of PowerShell.
Anyway, back to DSC.
DSC is a bit unique, because normally you don’t load resource modules; the Local Configuration Manager loads them. When you build a DSC resource class, you’re forced to provide three methods: Get(), Set(), and Test(). The LCM loads your module, instantiates the class, and then calls the three methods as needed. DSC resources built in this fashion can live in a plain old module .PSM1 file - there’s no need to create a DSCResources subfolder, no need to have an empty “root” module, or any of that. So it’s a more elegant solution all around. Aside from some structural differences, you code them the same as you always have. v5 still supports the old-style resources, for backward compatibility, but class-based resources are the “way forward.” I expect Microsoft will eventually refactor the DSC Resource Kit to be class-based resources, as soon as they get a minute and as soon as v5 is widely adopted.
So most of the “wiring” behind classes has, to this point, been designed to support that DSC use case. In other words, of all the things a PowerShell class will need to do, the team has so far focused mainly on those things that impact DSC. The rest will come later - the release notes use the phrase, “…in this release” a lot, meaning the team understands where the current weaknesses are. “This release” in some cases may simply mean _this current preview release, _meaning they’re targeting more features for v5’s final release; in other cases, more features will have to wait for v6 (or whatever) or a later version of PowerShell.
So there’s a little rambling on classes and what’s presently in PowerShell v5. If you haven’t already downloaded the preview and started playing with it, you should; _not in production, though. _Keep it in a test VM for the time being.

John Mello

PhillyPoSH 09/04/2014 meeting summary and presentation materials

  • Jan Egil Ring  gave a presentation entitled “Get Started with Windows PowerShell Desired State Configuration”. During his talked Jan went over a series of demos explaining how to use the configuration keyword to define configurations for different resources along with the different configuration modes. A copy of his demo scripts and presentation are available here.

Richard Siddaway
Announcements

LAST CALL for the European PowerShell Summit 2014

This is the last call for attendee registration for the European PowerShell Summit 2014.
The Summit is in Amsterdam - 29 September to 1 October 2014. Details from the events page https://powershell.org/community-events/summit/.
Due to a change in circumstances beyond our control we have to close public registration on 10 September 2014.
If you contact us by 10 September and ask to be able to perform a funds transfer rather than paying on line you have until 15 September 2014 to complete that transaction. No monies or registrations will be accepted after 15 September. We will not accept any new request for paying by money transfer after 10 September.
Apologies for the change in dates (the web site states registration is open until 15 September) but our hands have been forced on this.
There are still a number of places available so please register quickly if you want to attend. The more attendees we have the better chance we have of staging a European PowerShell Summit in 2015.

Don Jones
PowerShell Summit

PowerShell Summit Europe 2014: Prepare for the DSC Hackathon

We’re hoping that everyone attending the PowerShell Summit Europe 2014 will join our Monday evening **DSC Hackathon, **where we’ll become “product team members for a night” and try to code up some DSC Resources from the team’s own internal wish list!
We’ll provide a cash bar as well as finger food for our on-site attendees… but you’re welcome to participate remotely, too! Sometime on September 29th, watch PowerShell.org for a posting that includes the challenges. Choose your challenge, and follow the blog post instructions to submit them. We’ll also include details for participating live via IRC and other chat mechanisms, and we may be able to do a live room-cast via Lync or something.
There are no winners and no losers - only the _entire community _wins, because completed entries will be added to the PowerShell.org GitHub repo and made available to the world, for free. But, coders who complete a resource will receive public recognition, both here on PowerShell.org and in some other very visible venues!
Here’s what you’ll need to participate:

Richard Siddaway
Announcements

European Summit deadline approaching

There are just over two weeks left for you to register for the European PowerShell Summit. At the moment we are still short of the number that would enable us to repeat a European Summit in 2015. We had a lot of comments from people stating they wanted a Summit in Europe. Now is the time to step up and support that idea.
Hope to see you there

JasonMorgan
PowerShell for Admins

DenverPSUG – Keith Hill Presenting

Hello everyone,
The Denver PowerShell User Group will be meeting again on September 4th and we will have Keith Hill presenting. Keith has published an ebook, is a repeat Microsoft MVP, and has been heavily involved in writing and maintaining the PowerShell Community Extensions.
You can find more information on the event as well as RSVP here:

  Keith Hill - Effective PowerShell





  Thursday, Sep 4, 2014, 7:00 PM

899 Logan st

John Mello

Philadelphia Meeting – September 4th 2014

Join us Thursday, September 4th where Jan Egil RIng will be presenting a talk on Get Started with Windows PowerShell Desired State Configuration
Jan will explain how to use Windows PowerShell Desired State Configuration (DSC), which was introduced in Windows PowerShell 4.0, to configure your environment. The purpose of DSC is to provide Deployment, Configuration and Compliance capabilities for Windows resources such as a files, services, roles and features, users, groups and anything that can be managed from PowerShell by using custom resources such as a script. During his talk you will

Jacob Benson

Registration for August Omaha PowerShell User Group Meeting is Live!

https://www.eventbrite.com/e/omaha-powershell-users-group-august-meeting-tickets-12703856577

In the second ever meeting of the Omaha PowerShell User Group we will have PowerShell MVP Bartek Bielawski talking about OMI: PowerShell Everywhere:

  • CIM cmdlets and CDXML commands are advertised as technology that will enable PowerShell users to manage anything in datacenter. It wouldn’t be possible though without something that we can talk to on the remote end, and that’s were OMI kicks in. In this presentation I will show you how you can manage processes on Linux using OMI and CIM, and how easy it is to create CDXML based commands on top of it.

This meeting (and all future meetings) are for anyone who uses or is interested in PowerShell.

Jacob Benson

Omaha PowerShell User Group Meeting – 8/26

The next (and second ever) meeting of the Omaha PowerShell Users Group is taking place next Tuesday, August 26th.  MVP Bartek Bielawski will be talking about OMI on Windows and Linux.
I am having some issues with Lync in our Office 365 account so as soon as that gets straightened out I will be creating the invite for you to sign up, so watch for that!

Jacob Benson

Omaha PowerShell User Group Meeting Notes/Video

The first Omaha PowerShell User Group Meeting is in the books!  We had a great turnout with 26 people showing up last night.
The video Don Jones made for us is available on YouTube here.
Our next meeting will take place on August 26th with PowerShell MVP Bartek Bielawski doing the presentation on either Pre-Param Scriptology or PowerShell and OMI.  We will also have a short Scripting Game/Contest of some kind.  Stay tuned for the event sign up which should be going out soon.
If you would like to speak about PowerShell here is a list of some topics people have expressed interest in learning more about:
PowerShell Security/InfoSec
Desired State Configuration
OMI Interface w/PowerShell
Workflows/SMA
PowerCLI/VMWare
.NET Methods/Classes/Underneath/Exploration
OneGet/Chocolaty/Nuget
TFS/PowerShell Integration
PowerShell Formatting/Export Options
PowerShell Basics/PowerShell 101/Why PowerShell
Finally, if you have any ideas for things we could do for the Scripting Games/Contest please send them to omahapsug@gmail.com