The intern gets it !

April 23, 2008 at 3:04 PMAndy Schneider

A couple weeks ago we hired an intern. I recently gave him a task to create a bunch of DSN's on some servers. I told him to take some extra time and maybe figure out how to script it, and also let him know that I thought all the info was stored in the registry.

He came back into my office  later on in the day with a big smile exclaiming "This is awesome! Did you know you can navigate the registry just like a file system in Powershell ?"

I just love being there in the moment when the light bulb goes on and someone new "gets" Powershell. Its a beautiful thing!

Posted in:

Tags:

ScriptBlock Parameters

April 23, 2008 at 2:04 PMAndy Schneider

This was another one of those "Ah Hah!" moments with Powershell. Yesterday Jeffrey Snover wrote up a great post showing how to use ScriptBlocks as parameters in a pipeline.

This reminded me of a great podcast where Scott Hanselman interviewed Bruce Payette. Back when I first listened to it, they were talking about ScriptBlock parameters and it went completely over my head.

Now if you couple their discussion on the podcast with Jeffrey's post, you can come out with some incredibly powerful knowledge and understanding of Powershell.

Also, if you are not listening to Scott's podcast on a regular basis, I have one question for you. Why the heck not? He covers all kinds of great topics from HDTV to web development.

Posted in:

Tags:

Open Source Powershell on Mac and Linux

April 8, 2008 at 3:04 PMAndy Schneider

I am really looking forward to coming home and trying this out on my Mac. Igor  Moochnick has built Pash (Posh + Bash).  I can't wait to check it out. Perhaps I was wrong when I was discussing Text vs Objects.

I am really looking forward to seeing where this goes. Good stuff!

Posted in:

Tags:

The Powershell Function Directory

April 7, 2008 at 3:04 PMAndy Schneider

Anybody that has been using Powershell has created a function at some point. A function is simply a named ScriptBlock that can accept parameters. Also, you very likely know that there is a Function provider in Powershell. But I am willing to bet a lot of folks haven't used the provider all that much. If I am wrong, I would love to hear some feedback on the topic.

So here I am going to create a basic function and then we will look at what we can do with the function provider to manipulate it.

   1: 6 >  function foo {"Hello $args"}
   2: 7 >  foo world
   3: Hello world

So now that we have a new function we can cd into the function drive as follows. Notice that now we can get some cool info about the function we just created.

   1: 11 >  cd function:
   2: 12 >  ls foo | fl
   3:  
   4:  
   5: Name        : foo
   6: CommandType : Function
   7: Definition  : "Hello $args"

Since we are in the function directory, we can rename a function quite easily.

   1: 15 >  rename-item foo bar
   2: 16 >  bar world
   3: Hello world
   4:  
   5: 17 >  ls foo
   6: Get-ChildItem : Cannot find path 'foo' because it does not exist.
   7: At line:1 char:3
   8: + ls <<<<  foo
   9: 18 >  ls bar | fl
  10:  
  11:  
  12: Name        : bar
  13: CommandType : Function
  14: Definition  : "Hello $args"

There is no longer a function called foo but we do have a function named bar with the exact same definition that foo had originally.

Armed with this information, occasionally I like to take a look at what one of my functions looks like.

To do this all I need is to run get-content on a function, or use the alias cat.

   1: 28 >  cat Function:\Add-Assembly
   2: param($name) return [System.Reflection.Assembly]::LoadWithPartialName($name)

Posted in:

Tags: