0

I am trying to accomplish this:

When I insert a USB drive in Windows (Win7 and Win8), I want a script to be started (Powershell or batch command) automatically. The machine has autoplay disabled.

I setup a scheduled task with event id 2010 in DriveFrameworks-UserMode as trigger. The scheduled task works. However, I am having difficulties finding the drive letter of the newly inserted USB drive.

I found a few solutions on the Internet, but they didn't fit my requirements. The solutions either check for all drive letters (A to Z), or check for USB drive. I would like to correlate the drive letter that is associated with the event in the DriveFrameworks-UserMode trigger.

This post (How do i get the drive letter of a USB Drive in Powershell?) gave me some hints and I checked classes win32_logicaldisk, win32_diskdrive, and win32_pnpentity, but I couldn't find a clue matching them.

Any help is appreciated.

Community
  • 1
  • 1
Barry Chum
  • 829
  • 3
  • 14
  • 23

1 Answers1

1

This should help:

$diskdrive = gwmi win32_diskdrive | ?{$_.interfacetype -eq "USB"}
$letters = $diskdrive | %{gwmi -Query "ASSOCIATORS OF {Win32_DiskDrive.DeviceID=`"$($_.DeviceID.replace('\','\\'))`"} WHERE AssocClass = Win32_DiskDriveToDiskPartition"} |  %{gwmi -Query "ASSOCIATORS OF {Win32_DiskPartition.DeviceID=`"$($_.DeviceID)`"} WHERE AssocClass = Win32_LogicalDiskToPartition"} | %{$_. deviceid} 

$drive = gwmi win32_volume | ? {$letters -contains ($_.name -replace "\\")}
$drive.DriveLetter

PS. There is a similar discussion here

Community
  • 1
  • 1
Micky Balladelli
  • 9,781
  • 2
  • 33
  • 31
  • Thank you Micky. This is similar to the solution I posted in my original question. However, I am looking for more than that. My problem is, assume I already have a USB drive connected. When I insert a second USB drive, I need to find out the drive letter of the second USB drive. I was trying to match the deviceID or whatever using from wmi classes with the information found in the eventid 2010. However I failed. Thanks – Barry Chum Nov 23 '14 at 13:21