使用 Powershell 查找集线器插槽的驱动器盘符分配?

问题描述 投票:0回答:1

由于 Windows 为已安装的 USB 分配可用驱动器号的方式,我需要一种方法来告诉最终用户哪个 USB 位于 4 端口 USB 集线器的哪个插槽中。在 USB 海量存储设备下的设备管理器中,我看到 4 个“Port_#000x.Hub_#000y”形式的条目。所以我的问题是:Powershell 有什么方法可以获取每个集线器端口中安装的 USB(驱动器号或串行)?

此代码似乎可以可靠地识别集线器中使用的物理端口:

$Foo1 = Get-WmiObject Win32_diskdrive | Where-Object {$_.interfacetype -eq "USB"}

但是,尽管结果将驱动器称为“物理”,但它与插槽使用率并不是 1:1。例如,现在我使用物理插槽 1、3 和 4,但此命令返回的物理设备引用为 \.\PHYSICALDRIVE1、\.\PHYSICALDRIVE2 和 \.\PHYSICALDRIVE3。另一方面,USB 大容量存储设备下的设备管理器设备似乎是对物理插槽的持续引用。

理想情况下,结果将是对物理插槽的某种形式的引用以及驱动器号:

Slot                  Drive
------------------    -----
1                     D:
2                     G:
3                     F:
4                     E:

欢迎任何想法。我还发现我可以使用此命令返回的 deviceID 获取驱动器号(即 \.\PHYSICALDRIVEn:):

Get-WmiObject Win32_diskdrive | Where-Object {$_.interfacetype -eq "USB"}

但是该 deviceID 与此命令返回的设备 ID 之间没有明显的链接:

Get-CimInstance win32_PnPSignedDriver | where description -like 'USB Mass Storage Device'

如果有这样的链接会给我我正在寻找的连接。

powershell usb hub
1个回答
0
投票

我是这样做的:

#Get slots

$SlotNames = @(Get-CimInstance win32_PnPSignedDriver | where description -like 'USB Mass Storage Device' | select Location,deviceID)

#Get Physical Device Names

$USBRecords = wmic diskdrive where "DeviceID like '%PHYSICAL%' and not DeviceID like '%PHYSICALDRIVE0%'" get deviceID,PNPdeviceID| where {$_ -ne ""}| select-object -skip 1


#Get Physical device names to Drive letters

$DriveLetters = Get-WMIObject win32_diskdrive | ?{$_.interfacetype -eq "USB"} -pv disk |
 %{Get-WMIObject -Query "ASSOCIATORS OF {Win32_DiskDrive.DeviceID=`"$($_.DeviceID.replace('\','\\'))`"} WHERE AssocClass = Win32_DiskDriveToDiskPartition"} |
 %{Get-WMIObject -Query "ASSOCIATORS OF {Win32_DiskPartition.DeviceID=`"$($_.DeviceID)`"} WHERE AssocClass = Win32_LogicalDiskToPartition"} |
 select @{n='deviceid';e={$disk.deviceid}},@{n='driveletter';e={$_.deviceid}}

然后需要使用 USB 序列号来匹配记录,该序列号出现在插槽名称结果的位置 22 和物理设备名称结果的位置 65 中。上述操作需要注意的是,您需要知道 USB 集线器中的哪个端口是哪个端口。正如 @Mathias R. Jessen 上面提到的,插槽不一定按照您想象的顺序排列,您可以通过在安装在集线器各个插槽中的单个 USB 上运行上述命令来验证自己。

© www.soinside.com 2019 - 2024. All rights reserved.