Powershell get ipv4 address into a variable

后端 未结 12 2284
长情又很酷
长情又很酷 2021-01-31 08:23

Is there an easy way in powershell 3.0 Windows 7 to get the local computer\'s ipv4 address into a variable?

相关标签:
12条回答
  • 2021-01-31 09:04

    Here is another solution:

    $env:HostIP = (
        Get-NetIPConfiguration |
        Where-Object {
            $_.IPv4DefaultGateway -ne $null -and
            $_.NetAdapter.Status -ne "Disconnected"
        }
    ).IPv4Address.IPAddress
    
    0 讨论(0)
  • 2021-01-31 09:05

    If I use the machine name this works. But is kind of like a hack (because I am just picking the first value of ipv4 address that I get.)

    $ipaddress=([System.Net.DNS]::GetHostAddresses('PasteMachineNameHere')|Where-Object {$_.AddressFamily -eq "InterNetwork"}   |  select-object IPAddressToString)[0].IPAddressToString
    

    Note that you have to replace the value PasteMachineNameHere in the above expression

    This works too

    $localIpAddress=((ipconfig | findstr [0-9].\.)[0]).Split()[-1]
    
    0 讨论(0)
  • 2021-01-31 09:06

    I recently had the same issue. So I wrote a script to parse it from the ipconfig /all output. This script is easily modifiable to obtain any of the parameters of the interfaces and it works on Windows 7 also.

    1. Get output of IP config in LineNumber | Line format

    $ip_config = $(ipconfig /all | % {$_ -split "rn"} | Select-String -Pattern ".*" | select LineNumber, Line)

    1. Get list of interfaces (+ last line of ipconfig output) in LineNumber | Line format

    $interfaces = $($ip_config | where {$_.Line -notmatch '^\s*$'} | where {$_.Line -notmatch '^\s'}) + $($ip_config | Select -last 1)

    1. Filter through the interfaces list for the specific interface you want

    $LAN = $($interfaces | where {$_.Line -match 'Wireless Network Connection:$'})

    1. Get the start and end line numbers of chosen interface from output

    $i = $interfaces.IndexOf($LAN)
    $start = $LAN.LineNumber
    $end = $interfaces[$i+1].LineNumber

    1. Pick the lines from start..end

    $LAN = $ip_config | where {$_.LineNumber -in ($start..$end)}

    1. Get IP(v4) address field (returns null if no IPv4 address present)

    $LAN_IP = @($LAN | where {$_ -match 'IPv4.+:\s(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})'})
    $LAN_IP = &{If ($LAN_IP.Count -gt 0) {$Matches[1]} Else {$null}}

    0 讨论(0)
  • 2021-01-31 09:11

    Another variant using $env environment variable to grab hostname:

    Test-Connection -ComputerName $env:computername -count 1 | Select-Object IPV4Address
    

    or if you just want the IP address returned without the property header

    (Test-Connection -ComputerName $env:computername -count 1).IPV4Address.ipaddressTOstring
    
    0 讨论(0)
  • 2021-01-31 09:14
    $a = ipconfig
    $result = $a[8] -replace "IPv4 Address. . . . . . . . . . . :",""
    

    Also check which index of ipconfig has the IPv4 Address

    0 讨论(0)
  • 2021-01-31 09:15

    Here is what I ended up using

    $ipaddress = $(ipconfig | where {$_ -match 'IPv4.+\s(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})' } | out-null; $Matches[1])
    

    which breaks down as

    • execute ipconfig command - get all the network interface information
    • use powershell's where filter with a regular expression
    • regular expression finds the line with "IPv4" and a set of 4 blocks each with 1-3 digits separated by periods, i.e. a v4 IP address
    • disregard the output by piping it to null
    • finally get the first matched group as defined by the brackets in the regular expression.
    • catch that output in $ipaddress for later use.
    0 讨论(0)
提交回复
热议问题