在以下代码中,$ 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
}
答案 0 :(得分:3)
子网对IPv6的工作方式不同,因此您看到的流氓64是IPv6的子网掩码 - 而不是IPv4。
IPv6中的prefix-length相当于IPv4中的子网掩码。但是,不是像在IPv4中那样以4个八位字节表示,而是表示为1-128之间的整数。例如:2001:db8:abcd:0012 :: 0/64
为了删除它,您可以尝试以下方法(大量假设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
}
希望这有帮助!