使用 PowerShell 在设备管理器中显示的所有 COM 端口的列表 [英] List of all COM ports shown in device manager by using PowerShell

查看:270
本文介绍了使用 PowerShell 在设备管理器中显示的所有 COM 端口的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想合并一个下拉菜单,其中填充了可用 COM 端口的列表.我找不到任何方法可以轻松获取可用 COM 端口的名称以代替创建 $port 的 COM4.

I want to incorporate a dropdown menu that is populated with the list of available COM ports. I can't find any way to easily get the names of the available COM ports to put in the place of COM4 that creates the $port.

$port = new-Object System.IO.Ports.SerialPort COM4,​​19200,None,8,one

通过使用 Win32_SerialPort,我可以轻松提取 COM1 和 COM3.

By using Win32_SerialPort I am able to easily extract COM1 and COM3.

Get-WmiObject Win32_SerialPort | Select-Object deviceid

结果:

COM3
COM1

COM3
COM1

但我的设备管理器显示来自远程串行集线器的 16 个可用端口.设备管理器快照

But my device manager shows 16 available ports from a remote serial hub. Device Manager Snapshot

这是我尝试过的,我能够缩小名称的范围,但不知道如何仅提取 (COM--) 部分.

Here is what I have tried and I am able to narrow down the Name, but can't figure out how to extract just the (COM--) part.

Get-WmiObject Win32_pnpentity  -Filter "Name LIKE 'devicemaster port%'" | Select-Object -Property Name 

结果截图

推荐答案

让您自己解决一些工作,但根据您的结果屏幕截图,您可以执行以下操作:

Leaving some work to you to figure out, but based on your Result Screenshot you can do something like this:

$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"...

with that object, assuming you store that in '$ports'. You may need to use '$ports.Name'...

可能请参阅 regex101.com 了解正则表达式的工作原理.

Possibly see regex101.com for how the regex works.

这篇关于使用 PowerShell 在设备管理器中显示的所有 COM 端口的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆