I know it has been discussed that it is not possible to start an application from an usb drive upon connection due to limitations of autorun (or autoplay??) in Win 7. But it is possible to create a scheduled task having an event type trigger. Surely there must be an event occurring when the drive — or any USB device, for that matter — is connected.
Does anybody have the slightest idea which Event ID should I use? Or at least what type of event? Where can I find the event in event viewer?
Solution:
The thread Task Scheduler: How to automatically synchronize my USB flash drive?has this answer by a user called monotone, which uses PowerShell together withthe Task Scheduler :
I had the same question as you, and worked out something with powershell (windows built-in scripting) using techniques from the Scripting Guy Blog here and here. The script runs continuously as a background process, which you can start at system logon with task scheduler. The script will be notified whenever a new drive is plugged and then do something (here you configure the script rather than the task). Since it is basically paused while waiting for the next plugged drive, you should not find it takes up much resources. Here I go:
1) Start Powershell ISE, which can be found in your start menu under Accessories/Windows Powershell. 2) Copy paste the following into Powershell:
#Requires -version 2.0Register-WmiEvent -Class win32_VolumeChangeEvent -SourceIdentifier volumeChangewrite-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 = " $eventTypeNameif ($eventType -eq 2){$driveLetter = $newEvent.SourceEventArgs.NewEvent.DriveName$driveLabel = ([wmi]"Win32_LogicalDisk='$driveLetter'").VolumeNamewrite-host (get-date -format s) " Drive name = " $driveLetterwrite-host (get-date -format s) " Drive label = " $driveLabel# Execute process if drive matches specified condition(s)if ($driveLetter -eq 'Z:' -and $driveLabel -eq 'Mirror'){write-host (get-date -format s) " Starting task in 3 seconds..."start-sleep -seconds 3start-process "Z:sync.bat"}}Remove-Event -SourceIdentifier volumeChange} while (1-eq1) #Loop until next eventUnregister-Event -SourceIdentifier volumeChange
3) You need to modify the script above to tell the script what drive to look for, and what to execute. The two lines to change are:
if ($driveLetter -eq 'Z:' -and $driveLabel -eq 'Mirror')
My usb hard drive named ‘Mirror’ is set as the Z: drive. You could just use
if ($driveLabel -eq 'MyDiskLabel')
if you didn’t care about the letter.start-process "Z:sync.bat"
Path of whatever task you want to do. In my example, I have created a batch file on my USB drive which starts 3-4 backup tasks command lines.
4) When you’re done, save your script somewhere (extension
.ps1
), then go create a task in Task Scheduler to have your script run in background. Mine looks like this:
- Trigger: At log on
- Action: Start a program
- Program/script: powershell
- Add arguments:
-ExecutionPolicy Unrestricted -File "D:StuffBackup script.ps1"
5) Voilà!
6) Extra stuff:
If you want your script window to be hidden, use these arguments:
- Add arguments:
-WindowStyle Hidden -ExecutionPolicy Unrestricted -File "D:StuffBackup script.ps1"
If you want to output the script messages into a log file (that gets overwritten everytime the script starts, i.e. at log on), use the following task action:
- Program/script: cmd
- Add arguments:
/c powershell -WindowStyle Hidden -ExecutionPolicy Unrestricted -File "D:StuffBackup script.ps1" > "D:Stuffscript
log.txt”
Anytime you want to end the running hidden script, you can end the “Powershell” process in Task Manager.
The only downside is that nothing will run when you boot your computer with the drive already plugged in. (The script could be changed to perform a first check initially though, but I’ve had enough for today!)