mary Posted February 16, 2010 Share Posted February 16, 2010 Hi !my goal is to lock unautorised usb stick to plug. Here the steps:-Event: insertion of usb stick-read the usb stick disk identifier (I imagine is a unique serial number in USB' ROM ?)-if the identifier not exist in the whitelist (autorised.csv) then lock it (bloc pluging)thanks for any help Link to comment Share on other sites More sharing options...
Steveiwonder Posted February 16, 2010 Share Posted February 16, 2010 Maybe something like this could be of use. Usb modem, but its USB never the less. http://www.autoitscript.com/forum/index.php?showtopic=108798&st=0&p=766458&hl=USB&fromsearch=1&#entry766458 They call me MrRegExpMan Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted February 16, 2010 Moderators Share Posted February 16, 2010 mary, Here is some code to get the unique ID of USB sticks/drives attached to your machine. It should help you get started on your project! expandcollapse popup; credit spudw2k $aDrives = DriveGetDrive("Removable") For $i = 1 To $aDrives[0] $id = _GetPNPDeviceID($aDrives[$i]) MsgBox(0, $aDrives[$i], $id) Next Func _GetPNPDeviceID($drive, $fullid = 0) $objWMIService = ObjGet("winmgmts:{impersonationLevel=Impersonate}!\\.\root\cimv2") If Not IsObj($objWMIService) Then Return -1 ;Failed to Connect to WMI on Local Machine $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 ;Failed to Find Drive Letter $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 ;Failed to Find Physical Drive # $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 ;Failed to Find PNPDeviceID If Not $fullid Then $var = StringTrimLeft($var, StringInStr($var, "\", 0, -1)) ;Return Ugly Full PNPDeviceID Return $var EndFunc ;==>_GetPNPDeviceID M23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
Juvigy Posted February 16, 2010 Share Posted February 16, 2010 $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'") ;- End of setting up the WMI event ---------------- Sleep(111111) ;- The event that is called upon detecting a USB storage device. Func sink_OnObjectReady($objObject, $objAsyncContext) Local $choice If $objObject.TargetInstance.DriveType = 2 Then ;- TargetIstance.DriveType = 2 means a Removable drive. Select Case $objObject.Path_.Class () = "__InstanceCreationEvent" ;- Storage Device is plugged in. ConsoleWrite("Drive " & $objObject.TargetInstance.DeviceId & "has been inserted." & @CR) Case $objObject.Path_.Class () = "__InstanceDeletionEvent" ;- Storage device has been removed. ConsoleWrite("Drive " & $objObject.TargetInstance.DeviceId & "has been removed." & @CR) Case Else MsgBox(0,"","WTF?") EndSelect EndIf EndFunc This is how you detect when a usb drive is plugged . Link to comment Share on other sites More sharing options...
mary Posted February 16, 2010 Author Share Posted February 16, 2010 Thanks Melba23 and Juvigy, very helpfull ! Now, I will just find how to lock usb stick...and protect autorised.csv file to prevent modifying 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