【问题标题】:Get PID/VID of USB using Drive Letter使用 Drive Letter 获取 USB 的 PID/VID
【发布时间】:2022-01-06 13:25:07
【问题描述】:

我正在尝试创建一个将驱动器号(即 D:\ 或 E:\ 等)与 USB VID/PID(即“USB\VID_XXXX&PID_XXX\XXXXXXXXXXXXXXX”)相关联的脚本。 我尝试搜索整个互联网,但除了这里的一个之外找不到任何有希望的东西:See accepted answer at the top

这个答案能够解决这个问题,但我需要它在 powershell 中。我已尝试将代码转换为 powershell,但它不起作用 如果能提供任何帮助,我将不胜感激。

注意:您可以通过以下 sn-p 获取设备 InstanceID:

#Declaring variable & getting information about all the connected USB devices
$InstanceID = ""
$ConnectedDevices = Get-PnpDevice -Class "USB"

#Applying filter for USB Mass Storage Device and storing the instance ID to be used at later stage
ForEach($USB in $ConnectedDevices)
{
    If ($USB.FriendlyName -contains "USB Mass Storage Device")
        {  
            $InstanceID = $USB.DeviceID
        } 
}

这是不起作用的代码(或者您可以调用失败的工作):

$AllDevices = Get-CimInstance -ClassName Win32_USBHub
$InstanceID = "USB\VID_XXXX&PID_XXX\XXXXXXXXXXXXXXX"
$AllProperties
$USBObjects
$SecondIdentity
$AllDrives = Get-CimInstance -ClassName Win32_DiskDrive
Foreach($device in $AllDevices)
{
    If($device.DeviceID -like $InstanceID)
    {
        $AllProperties = $device
    }
}

Function FindPath
{
    ForEach($DeviceProperty in $AllProperties)
    {
        If($Device.Description -Contains "*USB Mass Storage Device")
        {
            $Entity = $Device.DeviceID

            ForEach($Controller in $Entity.GetRelated('Win32_USBController'))
            {
                $USBControllerID = gwmi win32_diskdrive | %{gwmi -Query "ASSOCIATORS OF {Win32_USBController.DeviceID='" + $Controller.DeviceID + "'} WHERE AssocClass = Win32_USBController"}
                ForEach($Obj in $USBControllerID)
                {
                    If($Obj -contains "Device ID")
                    {
                        $USBObjects = $Obj.DeviceID
                        $USBObjects 
                    }
                }
            }
        }

        $VidPidPosition = $USBObjects.Indexof($Entity)
        for($i = $VidPidPosition; $i -le $USBObjects.Count; $i++)
        {
            if($USBObjects.[i] -contains "USBSTOR")
            {
                $SecondIdentity = $USBObjects.[i]
            } 
        }
    }
}

Function GetDriveLetter
{
    FindPath
    ForEach($Drive in $AllDrives)
    {
        If($Drive.PNPDeviceID -eq $SecondIdentity)
        {
            ForEach($o in $Drive.GetRelated('Win32_DiskPartition'))
            {
                ForEach($i in $o.GetRelated('Win32_LogicalDisk'))
                {
                    Write-Output "Disk: " $i.Name
                }
            }
        }
    }
}
GetDriveLetter

提前致谢。

【问题讨论】:

    标签: windows powershell usb script


    【解决方案1】:

    经过一番头脑风暴,我可以找到一种更简单的方法来实现这一点,即将 USB 驱动器号与 PID/VID 相关联。 如果你注意到,在字符串“USB\VID_XXXX&PID_XXXX\123X5678X0987X543210XXXX”中,最后一个反斜杠后面的字符串,在“USBSTOR”字符串中是一样的,可以用来比较两者。

    以下是可用于从 txt 文件中读取驱动器号并将其与 PID/VID 进行比较的示例代码。最后,如果匹配,它将使用 PNPU​​til 执行断开连接和重新连接

        $ExitCode
    
    Function USBActions()
    {
        #Declaring variables, few of which will store all the information required to be used later
        $InstanceID = ""
        $ConnectedDevices = Get-PnpDevice -Class "USB"
        $UserSelection = ((Get-Content C:\SelectedDrive.txt).ToString()).Trim("\")
    
        $Disk = Get-WmiObject -Class Win32_logicaldisk -Filter "DriveType = '2'"
        $Driveletters = $Disk.DeviceID.ToString()
    
        $DiskPartition = $Disk.GetRelated('Win32_DiskPartition')
        $DiskDrive = $DiskPartition.getrelated('Win32_DiskDrive')
    
        #Applying filter for USB Mass Storage Device and storing the instance ID to be used at later stage
        ForEach($USB in $ConnectedDevices)
        {
            If ($USB.FriendlyName -contains "USB Mass Storage Device")
                {  
                    $InstanceID = $USB.DeviceID
                } 
        }
    
        If($InstanceID -eq "")
        {
            Write-Output "No removable mass storage device detected. Exiting..."
            $ExitCode = 1
            Return $Exitcode
        }
        Else
        {
            #For each connected mass storage device, iterate the drive letters in nested ForEach
            ForEach ($Instance in $InstanceID)
            {
                ForEach($Dsk in $DiskDrive)
                {
                  ForEach($DriveLetter in $DriveLetters)
                  {
                    #Check if drive letter from text file is same as the one Win32_DiskDrive class
                    If($UserSelection -match $Driveletter)
                    {
                        #Extract the string after last "\" in PID/VID instanceID & USBSTOR... string. 
                        $IndexOfIID = $InstanceID.LastIndexOf('\')
                        $USBSTOR = ($DiskDrive.PNPDeviceID).ToString()
                        $USBSTORCMP = $USBSTOR.LastIndexOf('\')
                        $ComparableString = $InstanceID.Substring(++$IndexOfIID)
    
                        #Compare the two
                        If($USBSTOR.Substring(++$USBSTORCMP) -match $ComparableString)
                        {
                            Write-Output "Drive matches DeviceID. Proceeding with disconnect & reconnect"
                        }
                    }
                    Else
                    {
                        Write-Output "Selected drive doesn't match device ID. Exiting..."
                        Exit
                    }
                  }
               }
                pnputil -Remove-Device $InstanceID
                Write-Log "Performed USB disconnect & reconnect for $InstanceID"
            }
    
            #Run scan for all the connected USB devices to reconnected the disconnected ones in previous step
            pnputil -scan-devices
            $ExitCode = 0
            Return $Exitcode
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-12
      • 2023-03-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多