Archive for June 23rd, 2008

PowerGUI TechEd Demo

As promised here is the demo I was doing in Jeffrey’s CTP2 talk at TechEd 2008.

The session was on PowerShell v2 and Jeffrey asked me to demo the new capabilities which v2 gives to UI applications. We selected background execution and eventing as the demo features.

The demo was very simple (in fact, thanks to the transparency of these features in v2 surprisingly so). I took the AD user provisioning action (which is a script action, and a fairly long and complicated one), used it on this scv file: presidents.csv

The provisioning took 20 seconds or so and because the execution was synchronous the UI was basically locked until the last presidential account was there. Not good!

But luckily the machine had PowerShell v2 installed so we went ahead and made this script a…

Background job

To do that, I opened the script (I was actually copy/pasting the script to a new Script Action), and added Start-PSJob as the first line, and then enclosed the whole script into a here-string parameter for the command:
Start-PSJob -Name Provision -Command @’
# here goes my script

Then I jut ran the new action the the UI was back right away! Meanwhile the whole provisioning was being done in the background!

I added a node to the PowerGUI console which was executing Get-PSJob and allowed me to see the status and results of any background jobs I had.

However, obviously another change made more sense – changing the system so the script would just run automatically whenever the CSV file with the information on the new AD accounts to be provisioned appears at a specific location. So we re-did the script – this time using


To do this we enclosed the script in a scriptblock (don’t ask me why scriptblocks are required for this one and strings for the previous one – I guess just different people architecting the features ;)) and added the commands creating a file system watcher and registering it with the new Register-ObjectEvent cmdlet:
$action = {
# here goes my script

$watcher = New-Object System.IO.FileSystemWatcher
$watcher.Path = c:\temp2
$watcher.Filter = *.csv
$watcher.EnableRaisingEvents = $true
Register-ObjectEvent $watcher Created -SourceIdentifier FileSystemWatcher.Created -Action $action

To check out all the triggers I had in place I created a Triggers node in PowerGUI using Get-PSEventSubscriber to show them all.

Now I copy/pasted my CSV to the folder the trigger was watching and all the accounts got provisioned!

Very simple!

As a bonus section here are a couple of tricks I used:

  1. I added Add-PSSnapin to the beginning of the script before doing the demo. This was to workaround the issue with background jobs not loading PowerShell profiles and not inheriting the loaded snapins from the current session.
  2. I hardcoded the path to the CSV file so I did not have to type the path each time I started the action, and did not have to make slightly more advanced things in the scripts (i.e. passing the parameter to the subscriber action and background job).

Apart from that – no modifications were required! I could just re-use my scripts in these new scenarios!

Tags: , , , ,

My Recent Tweets


The posts on this blog are provided “as is” with no warranties and confer no rights. The opinions expressed on this site are mine and mine alone, and do not necessarily represent those of my employer - WSO2 or anyone else for that matter. All trademarks acknowledged.

© 2007-2014 Dmitry Sotnikov

June 2008

%d bloggers like this: