1. ##########################################################################################
    2. ## -------------获取PC物理网卡IP.ps1-----------------
    3. ## 1. 将虚拟软件的虚拟网卡或VPN等非物理网卡IP排除在外;
    4. ## 2. 脚本最终解释权,归项目运营IT部所有
    5. ##########################################################################################
    6. function ip{
    7. $macadd = (Get-NetAdapter -Physical | ? Status -EQ "Up").MacAddress
    8. $address = foreach($address in (ipconfig /all) -like '*地址*') { ($address -split ' : ')[-1]}
    9. # $macadd使用ForEach-Object,是考虑到PC有多个物理网卡
    10. $macadd | ForEach-Object{
    11. # 将$_赋值给$macadd, 是为了和$address的$_做区分
    12. $macadd = $_
    13. $address | ForEach-Object{
    14. if($_ -like '*首选*'){
    15. $index = [array]::IndexOf($address, $_)
    16. $host_mac = $address[$($index-1)]
    17. if($host_mac -eq $macadd){
    18. $ip = ($_ -split '\(')[0]
    19. return $ip
    20. }
    21. }
    22. }
    23. }
    24. }