This week (of Jan 9, 2012) I am taking the Powershell v2.0 course at Benchmark Learning with eduator Lu Patrick (my colleague). It’s been a very interesting week. As a developer it’s actually been very easy to understand, pickup and use. In fact, with powershell leveraging the .NET framework it made it easy to understand 80% of the content. Now that said here are some thoughts.
What is my definition of powershell: Powershell is a scripting language that is deeply engrained in windows operating systems. It is a programming language for powerful automation, it can be used to interrogate and manipulate windows operating systems in a robust and scalable way.
Having achieved my Microsoft Certified Systems Engineer (MCSE) on NT4.0 in 2001 and working for a couple of years as a network engineer and IT manager, I was able to grasp some of the concepts and context on how and where to use Powershell. That said, this was also one of the hardest areas to understand. The coding, variables, scoping, and even piping was relatively easy. The questions around IT Pro job tasks were difficult. Such as, how do you find out membership information for a user in active directory or how do you use Powershell to remotely manage multiple servers.
The more I understand of Powershell, the more powerful I think it can be in my applications. For example I could use Powershell to automate the installation of my WCF service on multiple servers. However I would consider this as external to my application. We could use commandlets and powershell commands as a layer within our .NET applications. Then we could leverage the powerful interrogation techniques of powershell to, lets say, return server and process information for all servers within my environment to my ASP.NET MVC intranet application.
So about that Powershell? My take aways were three essential commands:
- Get-Help
- Get-Member
- Get-Command
Get-Help
Get-Help is a great way to find out more. As you’ll know/find in .NET, it’s impossible to think you can memorize the framework. So having commands like this help you interrogate the powershell framework to find out how to achieve a particular task.
Get-Member
As a developer I have always used MSDN and intellisense within Visual Studio to find out how to use classes and objects within the .NET framework. These features are also available in Powershell. For example:
In this example I’m using Get-Help and Get-Member in one attempt. Notice that TypeName looks very similar to a .NET namespace as you’d find on MSDN. In addition it provides synopsis, members and examples of how to implement it. In fact, Powershell is extensible and Lu Patrick showed us how to update powershell so you could call .MSDN() on your commandlet and it would open up an MSDN article on that topic.
Get-Member builds on top of Get-Help and provides you the methods and properties available on that object of choice.
Get-Command
Gets basic information about commandlets (cmdlets) and other elements of Windows PowerShell commands. The fun part about this is that Powershell will not only return the properties and methods of the objects available in the system but also massages those objects with addition powershell scripting options.
I can also pipe (concatenate) the results of Get-Member (using alias gm) to a list. Basically piping consists of building additional queries based on the results of a prior query/command. In this example I’ve taken the resulting Get-Process cmdlet and gathered all of the members (ie properties and methods) and display the results in a Formatted Table (ie ft).
What do these three key commands do for me?
This helps me solve problems until I’m comfortable at memorization. Because I don’t know what I don’t know. This is why this has become the biggest take away from the course.
.NET & Powershell
As a developer/network engineer I want to make sure both parties (from whichever side I’m on at the time) are happy. Developers should be able to build UIs in their rapid application environment (ie Visual Studio) and administrators should be able to automate tasks the application may perform. Not all environments allow for draggy droppy mouse clicky. Administrators like to interrogate, interact and automate applications the developers build. For the most part, most developers will not provide the powershell layer in their applications to return the favour.
Want to see how to create a custom powershell commandlet in .net? See my post .NET PowerShell Cmdlet and custom collection data.


