Jump to content
Sign in to follow this  
MadBoy

_GetPNPDeviceIDFromDriveLetter ($drive_letter)

Recommended Posts

MadBoy

Hey,

I wrote this to help a guy getting a PnPDeviceID from drive letter. Thought i would put it in Examples forum aswell as it might get lost in the General Forum :P It's not preety but i didn't found another way to write it ;) Enjoy

#include <String.au3>

$test = _GetPNPDeviceIDFromDriveLetter("H:")
ConsoleWrite($test)

Func _GetPNPDeviceIDFromDriveLetter($drive_letter)
    Local $drive_letter_found, $drive_letter_found
    $wbemFlagReturnImmediately = 0x10
    $wbemFlagForwardOnly = 0x20
    $colItems = ""
    $strComputer = "localhost"

    $objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")
    $colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_LogicalDiskToPartition", "WQL", $wbemFlagReturnImmediately + $wbemFlagForwardOnly)
    If IsObj($colItems) Then
        For $objItem In $colItems
            $LogicalDiskToPartitionAntecedent = _StringBetween($objItem.Antecedent, '"', '"')
            $LogicalDiskToPartitionDependent = _StringBetween($objItem.Dependent, '"', '"')
            $drive_statistics = $LogicalDiskToPartitionAntecedent[0]
            $drive_letter_found = $LogicalDiskToPartitionDependent[0]
            If $drive_letter = $drive_letter_found Then
                ExitLoop
            EndIf
        Next
    Else
        MsgBox(0, "WMI Output", "No WMI Objects Found for class: " & "Win32_LogicalDiskToPartition")
    EndIf
    If $drive_letter <> $drive_letter_found Then Return 0 ; If drive letter isn't function returns 0

    $colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_DiskDriveToDiskPartition", "WQL", $wbemFlagReturnImmediately + $wbemFlagForwardOnly)
    If IsObj($colItems) Then
        For $objItem In $colItems
            $DiskDriveToDiskPartitionAntecedent = _StringBetween($objItem.Antecedent, '"', '"')
            $DiskDriveToDiskPartitionDependent = _StringBetween($objItem.Dependent, '"', '"')
            $drive_statistics_found = $DiskDriveToDiskPartitionDependent[0]
            $drive_physical = StringTrimLeft($DiskDriveToDiskPartitionAntecedent[0], StringInStr($DiskDriveToDiskPartitionAntecedent[0], "\", 1, -1))
            If $drive_statistics = $drive_statistics_found Then
                ExitLoop
            EndIf
        Next
    Else
        MsgBox(0, "WMI Output", "No WMI Objects Found for class: " & "Win32_DiskDriveToDiskPartition")
    EndIf

    $colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_DiskDrive", "WQL", $wbemFlagReturnImmediately + $wbemFlagForwardOnly)
    If IsObj($colItems) Then
        For $objItem In $colItems
            $DeviceID = StringTrimLeft($objItem.DeviceID, StringInStr($objItem.DeviceID, "\", 1, -1))
            $PNPDeviceID = $objItem.PNPDeviceID
            If $drive_physical = $DeviceID Then
                Return $PNPDeviceID
            EndIf
        Next
    Else
        MsgBox(0, "WMI Output", "No WMI Objects Found for class: " & "Win32_DiskDrive")
    EndIf
EndFunc   ;==>_GetPNPDeviceIDFromDriveLetter

My little company: Evotec (PL version: Evotec)

Share this post


Link to post
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×