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

Eventing

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: , , , ,

2 Responses to “PowerGUI TechEd Demo”


  1. 1 Michael Greene June 23, 2008 at 6:18 pm

    This is really interesting and actually brings some ideas to mind. This would be really cool if employed as the background for a WPF application to retrieve web data and display it in a gadget.

  2. 2 dmitrysotnikov June 24, 2008 at 7:43 pm

    We actually considered using WPF in the demo but cut it to streamline the scenario and make sure we could fit everything in 5-10 minutes. I guess we had to leave something for IT Forum.😉


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s




My Recent Tweets

Legal

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
M T W T F S S
« May   Jul »
 1
2345678
9101112131415
16171819202122
23242526272829
30  

%d bloggers like this: