1

在下面的代码中,$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
}
4

1 回答 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
}

希望这可以帮助!

于 2013-07-15T01:40:39.130 回答