royalmarine Posted October 4, 2010 Share Posted October 4, 2010 (edited) Hi can someone help me with this? Im trying to make a program that when a usb key is inserted, it reads the unique id of the stick. If the unique id is not the same as my own, then it runs a security program. If it is the same, then it opens explorer. im very new to autoIT, so any help would be great! I found the code for the unique usb id from this thread http://www.autoitscript.com/forum/index.php?showtopic=110154&st=0&p=774103&hl=usb%20detect&fromsearch=1&#entry774103 so credit to the original author. expandcollapse popuptrComputer = "localhost" $objWMIServices = ObjGet("winmgmts:" & "{impersonationLevel=impersonate}!\\" & $strComputer & "\root\cimv2") $sink = ObjCreate("WbemScripting.SWbemSink") ObjEvent($sink, "SINK_") $objWMIServices.ExecNotificationQueryAsync($sink, "SELECT * FROM __InstanceOperationEvent WITHIN 5 WHERE " & "TargetInstance ISA 'Win32_LogicalDisk'") $aDrives = DriveGetDrive("Removable") For $i = 1 To $aDrives[0] $id = _GetPNPDeviceID($aDrives[$i]) Next Func _GetPNPDeviceID($drive, $fullid = 0) $objWMIService = ObjGet("winmgmts:{impersonationLevel=Impersonate}!\\.\root\cimv2") If Not IsObj($objWMIService) Then Return -1 $colDevice = $objWMIService.ExecQuery("SELECT * from Win32_LogicalDiskToPartition") $var = "" For $objItem In $colDevice If StringInStr($objItem.Dependent, $drive) Then $var = StringTrimLeft($objItem.Antecedent, StringInStr($objItem.Antecedent, "=")) EndIf Next If Not $var Then Return -2 $colDevice = $objWMIService.ExecQuery("SELECT * from Win32_DiskDriveToDiskPartition") $diskpartition = $var $var = "" For $objItem In $colDevice If StringInStr($objItem.Dependent, $diskpartition) Then $var = StringTrimLeft($objItem.Antecedent, StringInStr($objItem.Antecedent, "=")) EndIf Next If Not $var Then Return -3 $colDevice = $objWMIService.ExecQuery("SELECT * from Win32_DiskDrive") $physicaldrive = StringReplace(StringReplace($var, "\\", "\"), '"', "") $var = "" For $objItem In $colDevice If $objItem.DeviceID = $physicaldrive Then $var = $objItem.PNPDeviceID EndIf Next If Not $var Then Return -4 If Not $fullid Then $var = StringTrimLeft($var, StringInStr($var, "\", 0, -1)) Return $var EndFunc Func sink_OnObjectReady($objObject, $objAsyncContext) Local $choice If $objObject.TargetInstance.DriveType = 2 Then Select Case $objObject.Path_.Class () = "__InstanceCreationEvent" ; if $id = "J7CUV1T7&0" Then Run("explorer.exe " & "h:\") Else Run("C:\Users\mike\Desktop\Security.exe") EndIf ; Case $objObject.Path_.Class () = "__InstanceDeletionEvent" Case Else EndSelect EndIf EndFunc Edited October 4, 2010 by royalmarine Link to comment Share on other sites More sharing options...
wakillon Posted October 4, 2010 Share Posted October 4, 2010 I modify a bit... expandcollapse popupGlobal $_Drive $strComputer = "localhost" $objWMIServices = ObjGet("winmgmts:" & "{impersonationLevel=impersonate}!\\" & $strComputer & "\root\cimv2") $sink = ObjCreate("WbemScripting.SWbemSink") ObjEvent($sink, "SINK_") $objWMIServices.ExecNotificationQueryAsync($sink, "SELECT * FROM __InstanceOperationEvent WITHIN 5 WHERE " & "TargetInstance ISA 'Win32_LogicalDisk'") While 1 $aDrives = DriveGetDrive("Removable") For $i = 1 To UBound ( $aDrives ) -1 $_Drive = $aDrives[$i] $id = _GetPNPDeviceID($aDrives[$i]) ; ConsoleWrite ( $aDrives[$i] & " $id : " & $id & @Crlf ) Next Sleep ( 500 ) WEnd Func _GetPNPDeviceID($drive, $fullid = 0) $objWMIService = ObjGet("winmgmts:{impersonationLevel=Impersonate}!\\.\root\cimv2") If Not IsObj($objWMIService) Then Return -1 $colDevice = $objWMIService.ExecQuery("SELECT * from Win32_LogicalDiskToPartition") $var = "" For $objItem In $colDevice If StringInStr($objItem.Dependent, $drive) Then $var = StringTrimLeft($objItem.Antecedent, StringInStr($objItem.Antecedent, "=")) EndIf Next If Not $var Then Return -2 $colDevice = $objWMIService.ExecQuery("SELECT * from Win32_DiskDriveToDiskPartition") $diskpartition = $var $var = "" For $objItem In $colDevice If StringInStr($objItem.Dependent, $diskpartition) Then $var = StringTrimLeft($objItem.Antecedent, StringInStr($objItem.Antecedent, "=")) EndIf Next If Not $var Then Return -3 $colDevice = $objWMIService.ExecQuery("SELECT * from Win32_DiskDrive") $physicaldrive = StringReplace(StringReplace($var, "\\", "\"), '"', "") $var = "" For $objItem In $colDevice If $objItem.DeviceID = $physicaldrive Then $var = $objItem.PNPDeviceID EndIf Next If Not $var Then Return -4 If Not $fullid Then $var = StringTrimLeft($var, StringInStr($var, "\", 0, -1)) Return $var EndFunc Func sink_OnObjectReady($objObject, $objAsyncContext) Local $choice If $objObject.TargetInstance.DriveType = 2 Then Select Case $objObject.Path_.Class () = "__InstanceCreationEvent" if $id = "J7CUV1T7&0" Then Run("explorer.exe " & $_Drive & "\") Else Run("C:\Users\mike\Desktop\Security.exe") EndIf Case $objObject.Path_.Class () = "__InstanceDeletionEvent" Case Else EndSelect EndIf EndFunc AutoIt 3.3.14.2 X86 - SciTE 3.6.0 - WIN 8.1 X64 - Other Example Scripts Link to comment Share on other sites More sharing options...
royalmarine Posted October 4, 2010 Author Share Posted October 4, 2010 thank you wakillon, that worked perfectly! Link to comment Share on other sites More sharing options...
wakillon Posted October 4, 2010 Share Posted October 4, 2010 thank you wakillon, that worked perfectly!Glad to help you ! AutoIt 3.3.14.2 X86 - SciTE 3.6.0 - WIN 8.1 X64 - Other Example Scripts Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now