2013-07-14 178 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 
} 

希望這有助於!