问题描述
理想情况下,我想从ipconfig创建一个对象,该对象允许我们向下钻取每个适配器的属性,如下所示:$ ip.$ lan.$ mac作为lan适配器的mac地址.
Ideally, I would like to create an object from ipconfig that allows us to drilldown to each adapter's attributes like this: $ip.$lan.$mac for the lan adapter's mac address.
首先,我想开发一种方法来将每个适配器匹配将这3组(适配器类型,适配器名称,适配器属性)捕获到Powershell变量中(首选对象): https://regex101.com/r/wZ3sV1/1
To start, I would like to develop a way to capture these 3 groups (adapter type, adapter name, adapter attributes) per adapter match into Powershell variables (objects are preferred): https://regex101.com/r/wZ3sV1/1
这里有一些想法可以捕获以太网适配器"部分的三个部分,但是它们仅捕获以太网适配器本地连接:":
Here are some ideas to capture three parts of the Ethernet adapter section, but they are only capturing "Ethernet adapter Local Area Connection:":
$ip = ipconfig /all
$ip_lan = $ip | Select-String -pattern "(Ethernet [^a]*adapter) (Local[^:]+):\s*(([^\n]+\n)*)" -AllMatches | Foreach {$_.Matches} | ForEach-Object {$_.Value}
$regex_lan = [regex]"(Ethernet [^a]*adapter) (Local[^:]+):\n*(( +[^\n]+\n)*)"
$regex_lan.Matches($ip)
$regex_lan.Matches($ip).value
还有,有没有办法像这样捕获组提取的名称?:
Also, is there a way to capture the name of the group extraction like this?:
Description . . . . . . . . . . . : Realtek PCIe GBE Family Controller
成为描述= Realtek PCIe GBE系列控制器
becomes Description = Realtek PCIe GBE Family Controller
推荐答案
我知道这并不能直接回答您的问题.而不是使用Regex解析ipconfig
的输出.相反,您可以使用Get-NetIPConfiguration
来获取更易于处理的powershell对象.
I know this doesn't directly answer your question. But rather than parsing the output of ipconfig
with Regex. You could instead use Get-NetIPConfiguration
to get a powershell object that is easier to deal with.
PS> $ip = Get-NetIPConfiguration
PS> $ip
InterfaceAlias : Ethernet
InterfaceIndex : 12
InterfaceDescription : Intel(R) Ethernet Connection I217-LM
NetProfile.Name : xyz.com
IPv4Address : 10.20.102.162
IPv6DefaultGateway :
IPv4DefaultGateway : 10.20.102.1
DNSServer : 10.20.100.9
10.20.100.11
10.20.100.13
PS> $ip.IPv4Address
IPAddress : 10.20.102.162
InterfaceIndex : 12
InterfaceAlias : Ethernet
AddressFamily : IPv4
Type : Unicast
PrefixLength : 23
PrefixOrigin : Dhcp
SuffixOrigin : Dhcp
AddressState : Preferred
ValidLifetime : 05:16:53
PreferredLifetime : 05:16:53
SkipAsSource : False
PolicyStore : ActiveStore
PSComputerName :
因此,您可以执行以下操作以获取要获取的值.
Thus you can do the following to get the values you are looking to acquire.
$ip.InterfaceAlias
$ip.InterfaceDescription
$ip.IPv4Address.IPAddress
要获取MAC地址,可以使用Get-NetAdapter
cmdlet.
To get the MAC the address you can use the Get-NetAdapter
cmdlet.
$adapter = Get-NetAdapter
$adapter.MacAddress
您可以使用InterfaceIndex将这两个信息关联起来.然后返回一个哈希表,使每个哈希表都可访问.下面创建了这些组合对象的数组.
You can correlate the two pieces of information using the InterfaceIndex. Then return a hashtable that makes each accessible. The following creates an array of these combined objects.
$combined = Get-NetIPConfiguration | ForEach-Object {
$adapter = Get-NetAdapter -InterfaceIndex $_.InterfaceIndex
@{ IP = $_; Adapter = $adapter }
}
这篇关于捕获多行,多组到Powershell变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!