【问题标题】:Display NIC Info显示网卡信息
【发布时间】:2013-07-14 22:14:17
【问题描述】:

在下面的代码中,$ipAddress 存储了 IPV4 和 IPV6。我只希望显示IPV4,无论如何可以这样做吗?也许有分裂?

另外,子网掩码打印 255.255.255.0 64 - 这个流氓 64 来自哪里?

代码:

ForEach($NIC in $env:computername) {
    $intIndex = 1
    $NICInfo = Get-WmiObject -ComputerName $env:computername Win32_NetworkAdapterConfiguration | Where-Object {$_.IPAddress -ne $null}
    $caption = $NICInfo.Description 
    $ipAddress = $NICInfo.IPAddress
    $ipSubnet = $NICInfo.IpSubnet 
    $ipGateWay = $NICInfo.DefaultIPGateway 
    $macAddress = $NICInfo.MACAddress 
    Write-Host "Interface Name: $caption"
    Write-Host "IP Addresses: $ipAddress" 
    Write-Host "Subnet Mask: $ipSubnet"
    Write-Host "Default Gateway: $ipGateway"
    Write-Host "MAC: $macAddress"
    $intIndex += 1
}

【问题讨论】:

    标签: powershell


    【解决方案1】:

    子网对 IPv6 的工作方式不同,因此您看到的流氓 64 是 IPv6 的子网掩码,而不是 IPv4。

    IPv6 中的前缀长度相当于 IPv4 中的子网掩码。但是,它不像在 IPv4 中那样以 4 个八位字节表示,而是以 1-128 之间的整数表示。例如:2001:db8:abcd:0012::0/64

    请看这里:http://publib.boulder.ibm.com/infocenter/ts3500tl/v1r0/index.jsp?topic=%2Fcom.ibm.storage.ts3500.doc%2Fopg_3584_IPv4_IPv6_prefix_subnet_mask.html

    为了删除它,您可以尝试以下操作(大量假设 IPv4 始终排在第一位,但在我所有的实验中它还没有排在第二位;))

    ForEach($NIC in $env:computername) {
        $intIndex = 1
        $NICInfo = Get-WmiObject -ComputerName $env:computername Win32_NetworkAdapterConfiguration | Where-Object {$_.IPAddress -ne $null}
        $caption = $NICInfo.Description
        #Only interested in the first IP Address - the IPv4 Address
        $ipAddress = $NICInfo.IPAddress[0]
        #Only interested in the first IP Subnet - the IPv4 Subnet    
        $ipSubnet = $NICInfo.IpSubnet[0] 
        $ipGateWay = $NICInfo.DefaultIPGateway 
        $macAddress = $NICInfo.MACAddress 
        Write-Host "Interface Name: $caption"
        Write-Host "IP Addresses: $ipAddress" 
        Write-Host "Subnet Mask: $ipSubnet"
        Write-Host "Default Gateway: $ipGateway"
        Write-Host "MAC: $macAddress"
        $intIndex += 1
    }
    

    希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-03
      • 2022-01-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多