我提到了我能够search到的几个例子,这些例子看起来非常贴切,但是仍然无法得到这个工作。
我的input如下所示,从dhcp服务器统计命令,我已经确认$输出得到正确定义的行看起来像:
MIBCounts: Discovers = 63911. Offers = 3903. Delayed Offers = 0. Requests = 29199. Acks = 273080. Naks = 71. Declines = 0. Releases = 395. ServerStartTime = Tuesday, March 27, 2012 7:38:53 PM Scopes = 34. Scopes with Delay configured= 0. Subnet = 10.31.0.0. No. of Addresses in use = 203. No. of free Addresses = 40774. No. of pending offers = 0. Subnet = 10.32.3.0. No. of Addresses in use = 0. No. of free Addresses = 0. No. of pending offers = 0. Subnet = 10.32.100.0. No. of Addresses in use = 48. No. of free Addresses = 145. No. of pending offers = 0. Subnet = 10.32.101.0. No. of Addresses in use = 34. No. of free Addresses = 159. No. of pending offers = 0.
所以我试过的是这个,但没有匹配:
$output=$(netsh -r myserver dhcp server show mibinfo) $dhcp_regex=@" (?s)Subnet = (\d\.\d\.\d\.\d)\.\W+ .*No\. of Addresses in use = (\d+)\.\W+ .*No\. of free Addresses = (\d+)\.\W+ "@ $dhcp_record= { @{ subnet=$matches[0] inuse=$matches[1] free=$matches[2] }} $output -match $dhcp_regex $matches
援助感谢。
试试这个代替你的第一行:
(?s)Subnet = (\d+\.\d+\.\d+\.\d+)\.\W*
您还需要分别查看每行:
$output | % { $_ -match $dhcp_regex }
#追加| Out-Null 如果不希望每行都打印在屏幕上,则为| Out-Null 。
$匹配[1]
编辑:这是一个更完整的例子。
$dhcp_regex = 'Subnet = (\d+\.\d+\.\d+\.\d+)' $dhcp_regex2 = 'in use = (\d+)' $output | ? { $_ -match $dhcp_regex -or $_ -match $dhcp_regex2} | % { $Matches[1] }
编辑:这是一个多行的例子。
$dhcp_regex = '(?m)Subnet = (\d+\.\d+\.\d+\.\d+)\.\r\n.*in use = (\d+)' $output | Out-String | % { $_ -match $dhcp_regex } $matches
帽子提示: http : //www.vistax64.com/powershell/80160-powershell-regex-help.html
编辑:看起来像(?m)实际上是不必要的。 Out-String是秘密的酱油。
改变了你提供的正则expression式来添加额外的数据元素。
$dhcp_regex='(?m)Subnet = (\d+\.\d+\.\d+\.\d+)\.\r\n.*in use = (\d+)\.\r\n.* free Addresses = (\d+)\.'
现在工作正常 – 非常感谢! 当我无聊的时候,我可能会花一些时间去发现我们之前做的事情是多么微妙的失败。