Powershell脚本值获取

问题描述 投票:2回答:2

我想使用powershell脚本获取默认网关,我可以如下所示获取它。

Get-WmiObject -Class Win32_IP4RouteTable |
    where { $_.destination -eq '0.0.0.0' -and $_.mask -eq '0.0.0.0'} | 
        Sort-Object metric1 | select nexthop | select-object -first 1

结果

nexthop
-------
0.0.0.0

但是,我想只获取值“0.0.0.0”,而不是标题,任何解决方案?

powershell
2个回答
2
投票

您应该使用以下任一脚本获取属性值。

使用(your script).PropertyName

(Get-WmiObject -Class Win32_IP4RouteTable |
    where { $_.destination -eq '0.0.0.0' -and $_.mask -eq '0.0.0.0'} | 
        Sort-Object metric1 | select nexthop | select-object -first 1).nexthop

或者通过使用使用your script | select -ExpandProperty PropertyName

Get-WmiObject -Class Win32_IP4RouteTable |
    where { $_.destination -eq '0.0.0.0' -and $_.mask -eq '0.0.0.0'} | 
        Sort-Object metric1 | select nexthop | select-object -first |
            select -ExpandProperty nexthop

1
投票

您不必多次使用Select-Object cmdlet。

Get-WmiObject -Class Win32_IP4RouteTable -Filter "Destination = '0.0.0.0' AND Mask = '0.0.0.0'" |    
        Sort-Object metric1 | Select-Object -First 1 -ExpandProperty nexthop

要么

(Get-WmiObject -Class Win32_IP4RouteTable -Filter "Destination = '0.0.0.0' AND Mask = '0.0.0.0'" |    
        Sort-Object metric1 | Select-Object -First 1).nexthop
© www.soinside.com 2019 - 2024. All rights reserved.