2013-07-14 71 views
1

在以下代码中,$ ipAddress存储IPV4和IPV6。我只希望显示IPV4,无论如何,这可以做到吗?也许分裂?显示NIC信息

此外,子网掩码打印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 
} 

回答

3

子网的工作方式不同的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 
} 

希望这有助于!