我想获取默认网关的前 3 个八位字节


$g = (Get-WmiObject -Class Win32_IP4RouteTable |
     where { $_.destination -eq '0.0.0.0' -and $_.mask -eq '0.0.0.0'} |
     Sort-Object metric1 |
     select nexthop, metric1, interfaceindex).nexthop

这个命令给了我整个ip,就像192.168.1.1一样。

我对192.168.1.感兴趣.

一些字符串操作可以做到这一点:

$g.Substring(0, $g.LastIndexOf("."))
$ip=(Get-WmiObject -Class Win32_IP4RouteTable | where {'0.0.0.0' -in ( $_.destination, $_.mask)}).nexthop
# method 1
($ip -split ".")[0..2] -join "."

其他解决方案

$template=@"
{SubIP*:0.0.0}.0
{SubIP*:255.253.255}.255
"@
Get-WmiObject -Class Win32_IP4RouteTable | 
    where {'0.0.0.0' -in ( $_.destination, $_.mask)} | 
        %{ConvertFrom-String -TemplateContent $template -InputObject $_.nexthop}

相关内容

最新更新