Powershell 2.0 session MGT320 scripts and feedback

Posted on Posted in Uncategorized

As promised, here are the scripts I used for the PowerShell 2.0 session to create the event trigger and automatically create users in . If you remember I used an example of a file system event trigger to automatically create accounts in when a VSV was dragged into the directory. Prerequisite is PowerShell 2.0, and the PowerShell management add in from Quest.Com.

The first Script I have cunningly called Action.PS1. This script is registered and called any time the trigger occurs:

# Add beeps using the console beep function so we know when the trigger fires.  
[Console]::Beep(300, 1000)  
# This can slow your script down considerably.
# Only do this if adding from a PowerShell session without this extension loaded
Add-PSSnapin  Quest.ActiveRoles.ADManagement 
# Another beep to signal where we are in the script. 
[Console]::Beep(600, 1000)  
# Grap all CSV files. Note I’m not doing any fancy checking for formatting.   
# If anything is wrong or missing it will silently skip or fail. For each line create a user  
# Note this should all be one line. 
Import-CSV c:\temp2\*.csv | ForEach-Object { New-QADUser -Name $_.Name -SamAccountName $_.SamAccountName  -ParentContainer $_.ParentContainer -Title $_.Title -City $_.city -Firstname $_.Firstname -Lastname $_.Lastname;} 
# Beep again to show we’re at the end of the script 
[Console]::Beep(1600, 1000)  
# Delete all CSV files since were finished with them 
rm c:\temp2\*.csv


The Next Script we need will register the file system trigger. You can call this anything you like. Here is the code:

# Create a variable to encapsulate what the trigger action should do.
$action = {. c:\action.ps1}
# Create a variable for the file system watcher and populate it.
$watcher = New-Object System.IO.FileSystemWatcher
$watcher.Path = “c:\temp2“
$watcher.Filter = “*.csv“
# Switch eventing on
$watcher.EnableRaisingEvents = $true
# Register the event
Register-ObjectEvent $watcher “Created“ -SourceIdentifier “FileSystemWatcher.Created“ -Action $action

I want to thank my friend Dmitry for his original post, which gave me the idea for this awesome PowerShell 2.0 Eventing demo.