通过除Win32_NetworkAdapterConfiguration类之外的gwmi类获取IP地址

时间:2017-04-27 05:55:30

标签: powershell

我想使用GWMI通过powershell检索IP地址,但不使用Win32_NetworkAdapterConfiguration类。请帮忙。

1 个答案:

答案 0 :(得分:2)

有多种方法。这取决于您的PS版本和要求:

Dot Net方法

[net.dns]::GetHostAddresses("") | Select -ExpandProperty IPAddressToString

使用测试连接的原生PS命令(相当于ping):

Test-Connection -ComputerName ::1 -Count 1 |Select-Object -Property IPv*Address

来自注册表项:

Get-ChildItem -Path HKLM:\system\CurrentControlSet\services\Tcpip\Parameters\Interfaces| foreach{get-itemProperty $_.PSPath -N *IPAdd* -EA 0}|select *IP*

使用Ipconfig:

(ipconfig)-like'*IPv4*'|foreach{($_-split': ')[-1]}

如果您使用的是PS版本3或更高版本以及Windows 8 / Server 2012或更高版本,则可直接使用:

(Get-NetIPAddress).IPAddress(Get-NetIPConfiguration).IPv4Address

希望它有所帮助。

相关问题