I am using the following windows powershell script to detect when a particular volume is mounted so I can run a script that will move files from my machine to the device (I don't know much about powershell scripts, I found this online).
#Requires -version 2.0
Register-WmiEvent -Class win32_VolumeChangeEvent -SourceIdentifier volumeChange
write-host (get-date -format s) " Beginning script..."
do{
$newEvent = Wait-Event -SourceIdentifier volumeChange
$eventType = $newEvent.SourceEventArgs.NewEvent.EventType
$eventTypeName = switch($eventType)
{
1 {"Configuration changed"}
2 {"Device arrival"}
3 {"Device removal"}
4 {"docking"}
}
write-host (get-date -format s) " Event detected = " $eventTypeName
if ($eventType -eq 2)
{
$driveLetter = $newEvent.SourceEventArgs.NewEvent.DriveName
$driveLabel = ([wmi]"Win32_LogicalDisk='$driveLetter'").VolumeName
write-host (get-date -format s) " Drive name = " $driveLetter
write-host (get-date -format s) " Drive label = " $driveLabel
# Execute process if drive matches specified condition(s)
if ($driveLetter -eq 'G:' -and $driveLabel -eq 'My Book')
{
write-host (get-date -format s) " Starting task in 5 seconds..."
start-sleep -seconds 5
start-process "F:\copy_backups.bat"
}
}
Remove-Event -SourceIdentifier volumeChange
} while (1-eq1) #Loop until next event
Unregister-Event -SourceIdentifier volumeChange
G is a physical external hdd and F is a truecrypt container within G. When the script detects the correct device is mounted as G, it sleeps 5 seconds to give truecrypt time to mount F, and then runs the script found on F. It appears that volume change events are only generated when the physical drive is connected/disconnected (at least that's the only time the script receives an event), because leaving G connected and mounting/dismounting F does not trigger the script.
I would like to be able to detect when the truecrypt container is mounted without anything else changing. At some level this must be possible, because windows explorer updates its drives display when the container is mounted or dismounted. I read up on the win32_VolumeChangeEvent
, but I was unable to find anything about it in connection to virtual drives. Thanks for the help.