使用 PowerShell 在设备管理器中显示的所有 COM 端口的列表
List of all COM ports shown in device manager by using PowerShell
我想合并一个下拉菜单,其中填充了可用 COM 端口的列表。我找不到任何方法来轻松获取可用 COM 端口的名称,以代替创建 $port 的 COM4。
$port = new-Object System.IO.Ports.SerialPort COM4,19200,None,8,one
通过使用Win32_SerialPort,我可以轻松提取 COM1 和 COM3。
Get-WmiObject Win32_SerialPort | Select-Object deviceid
结果:
设备编号
COM3
COM1
但是我的设备管理器显示来自远程串行集线器的 16 个可用端口。
Device Manager Snapshot
这是我尝试过的方法,我能够缩小名称的范围,但不知道如何只提取 (COM--) 部分。
Get-WmiObject Win32_pnpentity -Filter "Name LIKE 'devicemaster port%'" | Select-Object -Property Name
Result Screenshot
留给您一些工作来解决,但根据您的结果屏幕截图,您可以执行以下操作:
$ports = @()
$ports += 'devicemaster port (COM1)'
$ports += 'devicemaster port (COM2)'
$ports += 'devicemaster port (COM3)'
$ports += 'devicemaster port (COM4)'
$ports | % {
if ($_ -match "devicemaster port \((.*)\)") {
$matches[1]
}
}
使用该对象,假设您将其存储在“$ports”中。您可能需要使用 '$ports.Name'...
可能请参阅 regex101.com 以了解正则表达式的工作原理。
添加一个迟到的答案,因为我刚好需要这个...
您可以使用 WMI ClassGuids 获取设备管理器显示的确切列表(COM 和 LPT):
$lptAndCom = '{4d36e978-e325-11ce-bfc1-08002be10318}'
get-wmiobject -Class win32_pnpentity | where ClassGuid -eq $lptAndCom | select name
确认可以使用一些 LPT / COM 扩展卡(Brain Boxes / Exar),使用 Windows 8.1 直至服务器 2019(Powershell 4 及以上)。
ClassGuids 的完整列表在这里:
https://docs.microsoft.com/en-us/windows-hardware/drivers/install/system-defined-device-setup-classes-available-to-vendors
这是获取 COM 端口详细信息的最新解决方案:
cls
$portList = get-pnpdevice -class Ports -ea 0
$portCount = 0
if ($portList) {
$now = get-date
foreach($device in $portList) {
$id = $device.InstanceId
if ($device.Present) {
$date = $now
} else {
$info = Get-PnpDeviceProperty -InstanceId $id
$latest = $info | ?{$_.KeyName -eq "DEVPKEY_Device_LastRemovalDate"}
$date = [datetime]$latest.Data
}
$age = $now-$date
if ($age.Days -lt 14) {
"port name : $name"
"last active: $date"
""
$portCount++
}
}
}
"number of active COM-port devices in last 14 days: $portCount"
我想合并一个下拉菜单,其中填充了可用 COM 端口的列表。我找不到任何方法来轻松获取可用 COM 端口的名称,以代替创建 $port 的 COM4。
$port = new-Object System.IO.Ports.SerialPort COM4,19200,None,8,one
通过使用Win32_SerialPort,我可以轻松提取 COM1 和 COM3。
Get-WmiObject Win32_SerialPort | Select-Object deviceid
结果:
设备编号
COM3
COM1
但是我的设备管理器显示来自远程串行集线器的 16 个可用端口。 Device Manager Snapshot
这是我尝试过的方法,我能够缩小名称的范围,但不知道如何只提取 (COM--) 部分。
Get-WmiObject Win32_pnpentity -Filter "Name LIKE 'devicemaster port%'" | Select-Object -Property Name
Result Screenshot
留给您一些工作来解决,但根据您的结果屏幕截图,您可以执行以下操作:
$ports = @()
$ports += 'devicemaster port (COM1)'
$ports += 'devicemaster port (COM2)'
$ports += 'devicemaster port (COM3)'
$ports += 'devicemaster port (COM4)'
$ports | % {
if ($_ -match "devicemaster port \((.*)\)") {
$matches[1]
}
}
使用该对象,假设您将其存储在“$ports”中。您可能需要使用 '$ports.Name'...
可能请参阅 regex101.com 以了解正则表达式的工作原理。
添加一个迟到的答案,因为我刚好需要这个...
您可以使用 WMI ClassGuids 获取设备管理器显示的确切列表(COM 和 LPT):
$lptAndCom = '{4d36e978-e325-11ce-bfc1-08002be10318}'
get-wmiobject -Class win32_pnpentity | where ClassGuid -eq $lptAndCom | select name
确认可以使用一些 LPT / COM 扩展卡(Brain Boxes / Exar),使用 Windows 8.1 直至服务器 2019(Powershell 4 及以上)。
ClassGuids 的完整列表在这里: https://docs.microsoft.com/en-us/windows-hardware/drivers/install/system-defined-device-setup-classes-available-to-vendors
这是获取 COM 端口详细信息的最新解决方案:
cls
$portList = get-pnpdevice -class Ports -ea 0
$portCount = 0
if ($portList) {
$now = get-date
foreach($device in $portList) {
$id = $device.InstanceId
if ($device.Present) {
$date = $now
} else {
$info = Get-PnpDeviceProperty -InstanceId $id
$latest = $info | ?{$_.KeyName -eq "DEVPKEY_Device_LastRemovalDate"}
$date = [datetime]$latest.Data
}
$age = $now-$date
if ($age.Days -lt 14) {
"port name : $name"
"last active: $date"
""
$portCount++
}
}
}
"number of active COM-port devices in last 14 days: $portCount"