Friday, October 30, 2009

PowerShell 2.0 RTM and WPK

So after several months of waiting PowerShell 2.0 for XP and Vista was released rather cryptically. It was released as part of the "Windows Management Framework on Windows XP, Windows Server 2003, Windows Vista, and Windows Server 2008." It is available for download here. I would have expected some sort of better announcement or explanation... canon fire or something. Oh well, it's here!

PowerShellPack
Microsoft has also released PowerShellPack. A collection of handy add ons to enhance your PowerShell experience. Included in PoweShellPack is a module called WPK, which bears a lot of resemblence to PowerBoots, that I have blogged about previously.  Here is a list of features in the PowerShellPack from the Microsoft PowerShell blog
@"
WPK
Create rich user interfaces quick and easily from Windows PowerShell. Think HTA, but easy. Over 600 scripts to help you build quick user interfaces.  To get started learning how to write rich WPF UIs in script, check out Writing User Interfaces with WPK.
IsePack
Supercharge your scripting in the Integrated Scripting Environment with over 35 shortcuts. TaskScheduler
List scheduled tasks, create or delete tasks
FileSystem
Monitor files and folders, check for duplicate files, and check disk space
DotNet
Explore loaded types, find commands that can work with a type, and explore how you can use PowerShell, DotNet and COM together
PSImageTools
Convert, rotate, scale, and crop images and get image metadata
PSRSS
Harness the FeedStore from PowerShell
PSSystemTools
Get Operating System or Hardware Information
PSUserTools
Get the users on a system, check for elevation, and start-processaadministrator
PSCodeGen
Generates PowerShell scripts, C# code, and P/Invoke

"@

Testing out WPK
So decided to give WPK a spin and do a simple image preview that I have done previously. It has left me scratching my head quite a bit.

The following works:
$width = 600        
$images = (ls *.jpg,*.png) | %{new-image $_.fullname -Width $width -Tag $_.Name}        
$text = $images |  %{New-Label $image.Tag -Width $width -FontSize 16}        
new-wrappanel {                     
  foreach($i in (0..($images.Count-1))){                                                           
    new-stackpanel {        
      $text[$i]        
      $images[$i]        
    }         
  }                            
}  -show        

While this does not:
function test-func        
{        
$width = 600        
$images = (ls *.jpg,*.png) | %{new-image $_.fullname -Width $width -Tag $_.Name}        
$text = $images | %{New-Label $image.Tag -Width $width -FontSize 16}        
new-wrappanel {                     
  foreach($i in (0..($images.Count-1))){                                                           
    new-stackpanel {        
      $text[$i]        
      $images[$i]        
    }         
 }                            
}  -show        
}        
test-func
 
The only difference between the two is that in the latter case the WPK calls are wrapped in a function. That's it. I'm totally confused as to why this might be happening.  This requires some head scratching. 

I'm curious to see how WPK and PowerBoots evolve in relation to each other.