在 powershell 中获取 ip 详细信息

getting ip details in powershell

我正在尝试从服务器上设置的 ipconfig 获取所有 IPv4 地址。

感谢 Chris 分享 info/solutions 获取 ip 详细信息的多种方法。

有效的解决方案:

Get-WmiObject @params | Select-Object NetConnectionID, `
        @{Name='IPAddress'; Expression={
            $_.GetRelated('Win32_NetworkAdapterConfiguration') | ForEach-Object {
                [IPAddress[]]$_.IPAddress | Where-Object { $_.AddressFamily -eq 'InterNetwork' }
            }
        }}

根据我上面的评论,这些示例使用 WMI 来提取接口名称和 IPv4 地址。

根据最重要的内容,可以采用不同的方法得出这一点。也就是选择过滤样式最方便的版本。

Win32_NetworkAdapterConfiguration 且 IPEnabled = TRUE

如果您对所有支持 IP 的接口感兴趣,这很有用。

Get-CimInstance Win32_NetworkAdapterConfiguration -Filter "IPEnabled='TRUE'" |
    Select-Object @{n='NetConnectionID';e={ ($_ | Get-CimAssociatedInstance -ResultClassName Win32_NetworkAdapter).NetConnectionID }},
                  @{n='IPAddress';e={ [IPAddress[]]$_.IPAddress | Where-Object AddressFamily -eq 'InterNetwork' }}

Win32_NetworkAdapter 和命名接口

如果您需要按名称定位接口并获取 IP,这可能是最好的选择。

Get-CimInstance Win32_NetworkAdapter -Filter "NetConnectionID='Ethernet'" |
    Select-Object NetConnectionID,
                @{n='IPAddress';e={
                    [IPAddress[]]($_ | Get-CimAssociatedInstance -ResultClassName Win32_NetworkAdapterConfiguration).IPAddress |
                        Where-Object AddressFamily -eq 'InterNetwork'
                }}

Get-WmiObject

Get-WmiObject 已被取代,但如果您更喜欢它的语法,这里又是第一个示例。

Get-WmiObject Win32_NetworkAdapterConfiguration -Filter "IPEnabled='TRUE'" |
    Select-Object @{n='NetConnectionID';e={ $_.GetRelated("Win32_NetworkAdapter") | ForEach-Object { $_.NetConnectionID } }},
                  @{n='IPAddress';e={ [IPAddress[]]$_.IPAddress | Where-Object { $_.AddressFamily -eq 'InterNetwork' } }}

使用System.Net.NetworkInformation

因为 WMI 不是唯一的方式,这也提供了直接访问,只需很少的工作。

$Name = 'MGT'
[System.Net.NetworkInformation.NetworkInterface]::GetAllNetworkInterfaces() |
    Where-Object Name -eq $Name |
    Select-Object Name,
            @{n='IPAddress';e={ $_.GetIPProperties().UnicastAddresses.Address |
                Where-Object AddressFamily -eq 'Internetwork'
            }}

Get-NetworkInterface 函数(PowerShell 2 兼容)

此功能允许您按名称选择网络接口,以便检查 IP 地址。默认情况下,它 returns 所有命名接口的信息。

function Get-NetworkInterface {
    param(
        [String]$Name = '*',

        [String]$ComputerName
    )

    $params = @{
        Class = 'Win32_NetworkAdapter'
    }
    if ($Name.IndexOf('*') -gt -1) {
        $Name = $Name -replace '\*', '%'
        $params.Add('Filter', "NetConnectionID LIKE '$Name'")
    } else {
        $params.Add('Filter', "NetConnectionID='$Name'")
    }
    if ($ComputerName -ne '') {
        $params.Add('ComputerName', $ComputerName)
    }

    Get-WmiObject @params | Select-Object NetConnectionID, `
        @{Name='IPAddress'; Expression={
            $_.GetRelated('Win32_NetworkAdapterConfiguration') | ForEach-Object {
                [IPAddress[]]$_.IPAddress | Where-Object { $_.AddressFamily -eq 'InterNetwork' }
            }
        }}
}

运营验证

这部分与 PowerShell 2 不兼容。这就是您如何利用单元测试框架(在本例中为 pester)来执行配置验证。

它显示了我想象您如何使用上述功能来验证 IP 配置。

Import-Module Pester
Describe 'SomeServer' {
    $ComputerName = 'SomeServer'

    Context 'Network configuration' {
        It 'Should have the management interface set to 10.1.2.3' {
            (Get-NetworkInterface -Name 'MGMT' -ComputerName 'SomeServer').IPAddress | Should Be '10.1.2.3'
        }
    }
}