Manage USB-Drives per devcon.exe

  • Ich habe das Script von eukalyptus an (AutoIt3.3.6.1) angepasst, das habe ich sogar hinbekommen und klappt auch. Jetzt würde ich noch gerne diese Abfrage mit rein nehmen, bekomme aber Syntaxfehler angezeigt und weiß nicht was falsch ist:

    [autoit]

    If Not FileExists($Devcon) > Then
    MsgBox(262192, "Fehler", "devcon.exe fehlt!")
    Else
    Exit
    EndIf

    [/autoit]


    Script eukalyptus:

    Spoiler anzeigen
    [autoit]


    ; ===================================================================================================
    ; Name...........: Manage USB Device
    ; Author ........: eukalyptus
    ; Date ..........: 2009, Feb 09th
    ; Modified.......:
    ; Remarks .......: manage USB Device with devcon.exe
    ; Related .......:
    ; Link ..........: http://www.autoit.de/index.php?page…evcon#post75965
    ; ===================================================================================================

    [/autoit] [autoit][/autoit] [autoit]

    #NoTrayIcon
    #include <GUIConstantsEx.au3>
    #include <GuiListView.au3>
    #include <ProgressConstants.au3>
    #include <Constants.au3>
    Opt("MustDeclareVars", 1)
    Opt("GuiOnEventMode", 1)

    [/autoit] [autoit][/autoit] [autoit]

    Global $Devcon = @SystemDir & "\devcon.exe"

    [/autoit] [autoit][/autoit] [autoit]

    Global $hGui, $hListView, $hProgress, $hDummyS, $hDummyE
    Global $aUSBDRIVES, $iPGCnt = 0, $B_DESCENDING

    [/autoit] [autoit][/autoit] [autoit]

    $hGui = GUICreate("Manage USB Device", 600, 225)
    GUISetOnEvent($GUI_EVENT_CLOSE, "_EXIT")
    $hListView = GUICtrlCreateListView("#|Name|Status|Bezeichnungs-ID", 10, 10, 450, 170, 0)
    _GUICtrlListView_SetColumnWidth($hListView, 0, 25)
    _GUICtrlListView_SetColumnWidth($hListView, 1, 200)
    _GUICtrlListView_SetColumnWidth($hListView, 2, 80)
    _GUICtrlListView_SetColumnWidth($hListView, 3, 400)
    $hDummyS = GUICtrlCreateDummy()
    GUICtrlCreateButton("Entfernen", 470, 10, 120, 20)
    GUICtrlSetOnEvent(-1, "_REMOVE")
    GUICtrlCreateButton("Restart", 470, 40, 120, 20)
    GUICtrlSetOnEvent(-1, "_RESTART")
    GUICtrlCreateButton("Aktivieren", 470, 70, 120, 20)
    GUICtrlSetOnEvent(-1, "_ENABLE")
    GUICtrlCreateButton("Deaktivieren", 470, 100, 120, 20)
    GUICtrlSetOnEvent(-1, "_DISABLE")
    GUICtrlCreateButton("Scannen", 470, 130, 120, 20)
    GUICtrlSetOnEvent(-1, "_SCAN")
    GUICtrlCreateButton("Liste aktualisieren", 470, 160, 120, 20)
    GUICtrlSetOnEvent(-1, "_UPDATE")
    $hDummyE = GUICtrlCreateDummy()
    $hProgress = GUICtrlCreateProgress(10, 190, 580, 20)
    GUISetState()

    [/autoit] [autoit][/autoit] [autoit]

    _UPDATE()

    [/autoit] [autoit][/autoit] [autoit]

    While 1
    Sleep(100)
    WEnd

    [/autoit] [autoit][/autoit] [autoit]

    Func _ButtonState($State = $GUI_ENABLE)
    For $i = $hDummyS To $hDummyE
    GUICtrlSetState($i, $State)
    Next
    GUICtrlSetState($hListView, $GUI_FOCUS)
    EndFunc ;=>_ButtonState

    [/autoit] [autoit][/autoit] [autoit]

    Func _Progress()
    $iPGCnt += 20
    If $iPGCnt > 100 Then $iPGCnt = 0
    GUICtrlSetData($hProgress, $iPGCnt)
    EndFunc ;=>_Progress

    [/autoit] [autoit][/autoit] [autoit]

    Func _DevCon($Cmd)
    _ButtonState($GUI_DISABLE)
    Local $aSel = _GUICtrlListView_GetSelectedIndices($hListView, True), $PID, $sStdOut
    GUICtrlSetStyle($hProgress, $PBS_MARQUEE)
    $iPGCnt = 0
    AdlibRegister("_Progress", 10)
    For $i = 1 To $aSel[0]
    $PID = Run($Devcon & " " & $Cmd & " " & _GUICtrlListView_GetItemText($hListView, $aSel[$i], 3), "", @SW_HIDE, $STDOUT_CHILD)
    $sStdOut = ""
    While 1
    $sStdOut &= StdoutRead($PID)
    If @error Then ExitLoop
    WEnd
    ProcessWaitClose($PID)
    If StringInStr($sStdOut, "No matching device") Then ContinueLoop
    If $Cmd = "remove" Then _GUICtrlListView_DeleteItem(GUICtrlGetHandle($hListView), $aSel[$i])
    If $Cmd = "disable" Then _GUICtrlListView_SetItemText($hListView, $aSel[$i], "DISABLED", 2)
    If $Cmd = "enable" Then _GUICtrlListView_SetItemText($hListView, $aSel[$i], "ENABLED", 2)
    Next
    AdlibUnRegister("_Progress")
    GUICtrlSetStyle($hProgress, 0)
    GUICtrlSetData($hProgress, 0)
    _ButtonState($GUI_ENABLE)
    EndFunc ;=>_DevCon

    [/autoit] [autoit][/autoit] [autoit]

    Func _UPDATE()
    _ButtonState($GUI_DISABLE)
    $aUSBDRIVES = _GetUSBDrives()
    For $i = 0 To _GUICtrlListView_GetItemCount($hListView)
    _GUICtrlListView_SetItemText($hListView, $i, "DISABLED", 2)
    Next
    _Add2LV($aUSBDRIVES)
    _ButtonState($GUI_ENABLE)
    EndFunc ;=>_UPDATE

    [/autoit] [autoit][/autoit] [autoit]

    Func _DISABLE()
    If Not FileExists($Devcon) > Then
    MsgBox(262192, "Fehler", "devcon.exe nicht gefunden!")
    Else
    Exit
    EndIf
    _DevCon("disable")
    EndFunc ;=>_DISABLE

    [/autoit] [autoit][/autoit] [autoit]

    Func _ENABLE()
    _DevCon("enable")
    EndFunc ;=>_ENABLE

    [/autoit] [autoit][/autoit] [autoit]

    Func _REMOVE()
    _DevCon("remove")
    EndFunc ;=>_REMOVE

    [/autoit] [autoit][/autoit] [autoit]

    Func _RESTART()
    _DevCon("restart")
    EndFunc ;=>_RESTART

    [/autoit] [autoit][/autoit] [autoit]

    Func _SCAN()
    _ButtonState($GUI_DISABLE)
    $iPGCnt = 0
    GUICtrlSetStyle($hProgress, $PBS_MARQUEE)
    AdlibRegister("_Progress", 10)
    RunWait($Devcon & " rescan", "", @SW_HIDE)
    AdlibUnRegister()
    GUICtrlSetStyle($hProgress, 0)
    GUICtrlSetData($hProgress, 0)
    _UPDATE()
    EndFunc ;=>_SCAN

    [/autoit] [autoit][/autoit] [autoit]

    Func _Add2LV($aUSB)
    If Not IsArray($aUSB) Then Return
    Local $iIndex
    For $i = 1 To $aUSB[0][0]
    $iIndex = _GUICtrlListView_FindInText($hListView, $aUSB[$i][2])
    If $iIndex >= 0 Then
    _GUICtrlListView_SetItemText($hListView, $iIndex, "ENABLED", 2)
    ContinueLoop
    EndIf
    $iIndex = _GUICtrlListView_AddItem($hListView, $aUSB[$i][0])
    _GUICtrlListView_AddSubItem($hListView, $iIndex, $aUSB[$i][1], 1)
    _GUICtrlListView_AddSubItem($hListView, $iIndex, "ENABLED", 2)
    _GUICtrlListView_AddSubItem($hListView, $iIndex, $aUSB[$i][2], 3)
    Next
    $B_DESCENDING = True
    _GUICtrlListView_SimpleSort($hListView, $B_DESCENDING, 1)
    EndFunc ;=>_Add2LV

    [/autoit] [autoit][/autoit] [autoit]

    Func _GetUSBDrives($ComputerName = ".")
    Local $wmiServices, $wmiUsbDevice, $wmiDiskDrive, $wmiDiskPartitions, $wmiLogicalDisks
    Local $USBDevice, $DiskDrive, $DiskPartition, $LogicalDisk, $aRet[1][3], $iCnt = 0
    Local $iSteps = 0, $iStep = 0
    $wmiServices = ObjGet("winmgmts:{impersonationLevel=Impersonate}!//" & $ComputerName)
    $wmiUsbDevice = $wmiServices.ExecQuery("SELECT * FROM Win32_USBControllerDevice")
    For $USBDevice In $wmiUsbDevice
    $iSteps += 1
    Next
    For $USBDevice In $wmiUsbDevice
    $iStep += 1
    GUICtrlSetData($hProgress, Round($iStep * 100 / $iSteps))
    $wmiDiskDrive = $wmiServices.ExecQuery("ASSOCIATORS OF {Win32_PnPEntity.DeviceID=" & StringTrimLeft($USBDevice.Dependent, StringInStr($USBDevice.Dependent, "=")) & "} WHERE ResultClass = Win32_DiskDrive")
    For $DiskDrive In $wmiDiskDrive
    $wmiDiskPartitions = $wmiServices.ExecQuery("ASSOCIATORS OF {Win32_DiskDrive.DeviceID='" & $DiskDrive.DeviceID & "'} WHERE AssocClass = Win32_DiskDriveToDiskPartition")
    For $DiskPartition In $wmiDiskPartitions
    $wmiLogicalDisks = $wmiServices.ExecQuery("ASSOCIATORS OF {Win32_DiskPartition.DeviceID='" & $DiskPartition.DeviceID & "'} WHERE AssocClass = Win32_LogicalDiskToPartition")
    For $LogicalDisk In $wmiLogicalDisks
    $iCnt += 1
    ReDim $aRet[$iCnt + 1][3]
    $aRet[$iCnt][0] = $LogicalDisk.DeviceID
    $aRet[$iCnt][1] = $DiskDrive.Caption
    $aRet[$iCnt][2] = '"@' & StringTrimLeft(StringReplace(StringTrimLeft($USBDevice.Dependent, StringInStr($USBDevice.Dependent, "=")), "\\", "\"), 1)
    Next
    Next
    Next
    Next
    $aRet[0][0] = $iCnt
    GUICtrlSetData($hProgress, 0)
    Return $aRet
    EndFunc ;=>_GetUSBDrives

    [/autoit] [autoit][/autoit] [autoit]

    Func _EXIT()
    Exit
    EndFunc ;=>_EXIT

    [/autoit]


    Sollte ja für die Func _DISABLE(), _ENABLE(), _REMOVE(), _RESTART() sein, k.A. ob man das noch einfacher hinbekommt
    und nur einmal auflisten braucht, anstelle das viermal so zu schreiben, wenn es denn so funktioniert!

    Über eine Antwort würde ich mich freuen.

  • OK danke, das war der Übeltäter. :)

    Bleibt noch die Frage, wie man das nur einmal aufführen braucht mit einer Variablen oder sowas!?
    Dann kann man es für diese Func _DISABLE(), _ENABLE(), _REMOVE(), _RESTART() einsetzen.