在下面的代码中,$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
}发布于 2013-07-15 09:40:39
对于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
}希望这能有所帮助!
https://stackoverflow.com/questions/17644341
复制相似问题