Using Hash Tables as Enums

In C# you can build an Enum that essentially makes your code much easier to read.


This is especially useful for bit flags where you want to represent something that is quite readable but in the background there could be all kinds ugly data that is not easy to remember.

We can do something similar in PowerShell using a hash table. For example, lets say we have a set of temperatures that we want to refer to throughout a script.

$temps = @{}

$temps.freezing = 32

$temps.boilng = 212

$temps.absoluteZero = −459.67

$temps.comfortable = 72


From here you you could take a variable $temp and say something like

if ($temp -lt $temps.freezing) {"Watch for Snow!"}

if ($temp -lt $temps.absoluteZero) {"You broke physics"}


This just may be a way to make scripts a bit more readable and easier to update after the fact.



Fibonacci Series in Powershell

Scott Hanselman recently posted The Weekly Source 13 - Fibonacci Edition. . It was a very interesting read, but it was missing the Powershell version. As in the Ruby example, you can use multiple assignments in Powershell as well
Function Get-Fib ($n) {
     $current = $previous = 1;
     while ($current -lt $n) {
           $current,$previous = ($current + $previous),$current}
Get-Fib 100
Bruce's book, Powershell in Action, uses a slightly more terse version of this function in his discussion of multiple variable assignment.

.NET Rocks ! – IT Pros AND Developers

The concept of IT Pros VS Developers is an idea that simply needs to die. IT Guys/Gals need Developers and Developers need IT folks.

I was listening to .NET Rocks the other day. As they were reading one of their fan's emails on the air I thought it sounded really familiar. Pretty quickly I realized that they were reading an email I had sent them about a month ago and had forgotten about.

Basically, my email was talking about how using Powershell has enabled me to jump into the world of .NET and start playing around with C#. This has enabled me to have much more meaningful conversations with Developers in my department. I have been learning their language, if you will.

The way I see it, the IT Pro is going to be doing more and more automation and the more we know about writing code, or at least how it is written, can only help us in our careers.

As IT Pros, we can have discussions about instrumentation requirements for an app that we will need to support way more effectively if we understand the basic capabilities of building instrumentation and management into an app in the first place. Maybe its getting familiar with System.Diagnostics.EventLog or learning a little bit about tracing. Maybe its understanding how MOM management packs are created. With SCOM 2007, developers can very easily create management packs now.

In fact, Carl and Richard were talking to David Aiken about this very topic on this episode of DNR

You know you are addicted to coffee when …

You pull up to your local Starbucks drive through and order a Venti drip.  When you pull up to the window to pay and pick up your coffee, you notice you have an old cup of coffee in your cup holder. It just so happens to be another Starbucks cup. You politely ask the barista if she would be so kind as to throw it away and laughing, she graciously obliges.

Generating a list of IP Addresses

Quite often, for one reason or another, I need to create a list of IP Addresses. The Powershell Range Operator ".." comes in handy for such a situation. To get a list of all the IP's in I can run the following command


[sourcecode language='css'] $ips = 1..254 | % {"10.10.10.$_"} [/sourcecode]


Now I have an array of my IP's that I can work with

Adding Line Numbers to Native Command Output

One of the great things about Powershell is that you can use all your standard command line tools, such as ipconfig and net.exe. The only issue is that these tools output text and Powershell works with objects. The output of commands such as ipconfig is actually an array.
PS 24 >  (ipconfig).GetType().BaseType.FullName
Each item in that array is a string
PS 31 >  (ipconfig)[4].gettype().FullName
So really you end up with an array of strings and if you know the indexes of that array, you can work with much more specific data. For example:
PS 58 >  (ipconfig)[7]
   IPv4 Address. . . . . . . . . . . :
But how do you know that this is line 7, or the 7th item in the array. You can run the command and count lines and guess, or you can do something like this:

PS 57 >  $i = 0;ipconfig| % {$i++;"$($i-1) `t $_"}
1        Windows IP Configuration
4        Ethernet adapter Local Area Connection:
6           Connection-specific DNS Suffix  . :
7           IPv4 Address. . . . . . . . . . . :
8           Subnet Mask . . . . . . . . . . . :
9           Default Gateway . . . . . . . . . :
11       Tunnel adapter Local Area Connection* 6:
13          Media State . . . . . . . . . . . : Media disconnected
14          Connection-specific DNS Suffix  . :
16       Tunnel adapter Local Area Connection* 7:
18          Media State . . . . . . . . . . . : Media disconnected
19          Connection-specific DNS Suffix  . :
Now you can know for sure what line you need to access and work with.

(ipconfig)[19] will give me my DNS name.

The other way around this is working with regular expressions to parse the file, but this will get you something really quickly. It works for a bunch of commands as well.

If you are really up for "objectifying" text output, check out Lee Holme's Awk with a Vengeance


Powershell or Ruby ?

Recently I have been playing with Ruby and I have to say the language is a ton of fun. This one line of code pretty much sold me on the language.   5.times {puts "Hello World"} Similar to Powershell, in Ruby, everything is an object, including primitive times. In Powershell, I could definitely get into some XML goo and spot-weld a method to System.Int32 called times. What would be really cool is if in Powershell I could expand the int32 class on the fly and not have to mess with XML, just as I can in Ruby. mystring = "Hello There" puts mystring.length class String  def myownmethod   puts "Methods on the fly are pretty cool"  end end puts mystring.myownmethod Below is the output from the code RubyMate r6075 running Ruby r1.8.6 (/usr/bin/ruby) messwithruby.rb 11 Methods on the fly are pretty cool nil I love Powershell and am a die-hard fan. When it comes to the debate of Powershell vs Ruby I personally take Scott Hanselman's suggestion. Learn them both and you wil be a better scripter/developer/person. Sure, you can also use add-member for instances of objects, but to be able to create and modify classes in Powershell V2 would be pretty darn cool.