The new eventing infrastructure in PowerShell 2.0 is pretty delicious. You couldn’t do the following in 1.0 without a 3rd party snap-in (like my PSEventing snapin), but now it’s all there at the touch of your fingers. Well, it demands a bit of a sniff around WMI too, but hey, it works well. With this module, anytime you add or remove a removable device like an external harddrive or USB key, or map a new network drive in explorer, PowerShell will now automatically add or remove a corresponding PSDrive for you.
-
-
-
- $query = new-object System.Management.WqlEventQuery
- $query.EventClassName = "__InstanceOperationEvent"
-
-
- $query.WithinInterval = new-object System.TimeSpan 0,0,2
-
-
-
- $query.QueryString = "Select * from Win32_VolumeChangeEvent"
-
-
- $watcher = new-object System.Management.ManagementEventWatcher $query
-
-
-
-
-
-
- Register-ObjectEvent $watcher -EventName "EventArrived" `
- -SupportEvent "WMI.VolumeChange" -Action {
- & (get-module automount) VolumeChangeCallback @args
- }
-
-
-
-
-
-
-
-
-
- $eventTypes = @{
- 1 = "ConfigurationChanged";
- 2 = "Arrived";
- 3 = "Removed";
- 4 = "Docking";
- }
-
-
- function VolumeChangeCallback ($sender, $eventargs) {
- trap { write-warning $_ }
-
- $driveName = $eventArgs.NewEvent.DriveName.TrimEnd(":")
- $eventType = [int]$eventArgs.NewEvent.EventType
-
- $forwardedEvent = "Device$($eventTypes[$eventType])"
-
-
- [void]( New-PSEvent "PowerShell.$forwardedEvent" -Sender $driveName `
- -EventArguments $eventargs )
- }
-
-
-
- function Enable-AutoMount {
-
- Register-PSEvent -SourceIdentifier "PowerShell.DeviceArrived" `
- -Action {
- new-psdrive -name $args[0] -psprovider `
- filesystem -root "$args[0]:";
- }
-
- Register-PSEvent -SourceIdentifier "PowerShell.DeviceRemoved" `
- -Action {
- remove-psdrive -name $args[0] -ea 0;
- }
-
- $watcher.Start()
- }
-
-
-
- function Disable-AutoMount {
-
- Unregister-PSEvent -SourceIdentifier "PowerShell.DeviceArrived"
- Unregister-PSEvent -SourceIdentifier "PowerShell.DeviceRemoved"
-
- $watcher.Stop()
- }
-
-
- Export-ModuleMember Enable-AutoMount, Disable-AutoMount
-
-
- Enable-AutoMount
This only works PowerShell v2.0 CTP2, and you’ll need to save it as AutoMount.psm1 in a directory under your documents folder like so (vista example):
%userprofile%\documents\windowspowershell\packages\automount\automount.psm1
You can then load it with the command:
ps> add-module automount
I have this in my profile. You can temporarily disable automount with the function Disable-AutoMount and reenable it at anytime with Enable-AutoMount. The module also exposes four new events for you to consume yourself. You could, for example, hook your own script to run anytime a device is added and/or removed. This is what I do myself in the module. I hook a WMI event once then forward 1 of 4 possible new events depending on the type of WMI event that was raised.
NOTE: this particular flavour of WMI query only works in Vista and Windows 2003 it appears. I’m looking into getting it working with 2000/XP also.
Have fun!