Powershell get ipv4 address into a variable

后端 未结 12 2285
长情又很酷
长情又很酷 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:16

    tldr;

    I used this command to get the ip address of my Ethernet network adapter into a variable called IP.

    for /f "tokens=3 delims=: " %i  in ('netsh interface ip show config name^="Ethernet" ^| findstr "IP Address"') do set IP=%i
    

    For those who are curious to know what all that means, read on

    Most commands using ipconfig for example just print out all your IP addresses and I needed a specific one which in my case was for my Ethernet network adapter.

    You can see your list of network adapters by using the netsh interface ipv4 show interfaces command. Most people need Wi-Fi or Ethernet.

    You'll see a table like so in the output to the command prompt:

    Idx     Met         MTU          State                Name
    ---  ----------  ----------  ------------  ---------------------------
      1          75  4294967295  connected     Loopback Pseudo-Interface 1
     15          25        1500  connected     Ethernet
     17        5000        1500  connected     vEthernet (Default Switch)
     32          15        1500  connected     vEthernet (DockerNAT)
    

    In the name column you should find the network adapter you want (i.e. Ethernet, Wi-Fi etc.).

    As mentioned, I was interested in Ethernet in my case.

    To get the IP for that adapter we can use the netsh command:

    netsh interface ip show config name="Ethernet"

    This gives us this output:

    Configuration for interface "Ethernet"
        DHCP enabled:                         Yes
        IP Address:                           169.252.27.59
        Subnet Prefix:                        169.252.0.0/16 (mask 255.255.0.0)
        InterfaceMetric:                      25
        DNS servers configured through DHCP:  None
        Register with which suffix:           Primary only
        WINS servers configured through DHCP: None
    

    (I faked the actual IP number above for security reasons

    0 讨论(0)
  • 2021-01-31 09:21
    (Get-WmiObject -Class Win32_NetworkAdapterConfiguration | where {$_.DefaultIPGateway -ne $null}).IPAddress | select-object -first 1
    
    0 讨论(0)
  • 2021-01-31 09:21

    Here are three methods using windows powershell and/or powershell core, listed from fastest to slowest. You can assign it to a variable of your choosing.



    Method 1: (this method is the fastest and works in both windows powershell and powershell core)
    $ipAddress = (Get-NetIPAddress | Where-Object {$_.AddressState -eq "Preferred" -and $_.ValidLifetime -lt "24:00:00"}).IPAddress
    


    Method 2: (this method is as fast as method 1 but it does not work with powershell core)
    $ipAddress = (Test-Connection -ComputerName (hostname) -Count 1 | Select -ExpandProperty IPv4Address).IPAddressToString
    


    Method 3: (although the slowest, it works on both windows powershell and powershell core)
    $ipAddress = (Get-NetIPConfiguration | Where-Object {$_.IPv4DefaultGateway -ne $null -and $_.NetAdapter.status -ne "Disconnected"}).IPv4Address.IPAddress
    

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

    To grab the device's IPv4 addresses, and filter to only grab ones that match your scheme (i.e. Ignore and APIPA addresses or the LocalHost address). You could say to grab the address matching 192.168.200.* for example.

    $IPv4Addr = Get-NetIPAddress -AddressFamily ipV4 | where {$_.IPAddress -like X.X.X.X} | Select IPAddress
    
    0 讨论(0)
  • 2021-01-31 09:22

    How about this? (not my real IP Address!)

    PS C:\> $ipV4 = Test-Connection -ComputerName (hostname) -Count 1  | Select IPV4Address
    
    PS C:\> $ipV4
    
    IPV4Address                                                  
    -----------
    192.0.2.0
    

    Note that using localhost would just return and IP of 127.0.0.1

    PS C:\> $ipV4 = Test-Connection -ComputerName localhost -Count 1  | Select IPV4Address
    
    PS C:\> $ipV4
    
    IPV4Address                                                             
    -----------                                                  
    127.0.0.1
    

    The IP Address object has to be expanded out to get the address string

    PS C:\> $ipV4 = Test-Connection -ComputerName (hostname) -Count 1  | Select -ExpandProperty IPV4Address 
    
    PS C:\> $ipV4
    
    Address            : 556228818
    AddressFamily      : InterNetwork
    ScopeId            : 
    IsIPv6Multicast    : False
    IsIPv6LinkLocal    : False
    IsIPv6SiteLocal    : False
    IsIPv6Teredo       : False
    IsIPv4MappedToIPv6 : False
    IPAddressToString  : 192.0.2.0
    
    
    PS C:\> $ipV4.IPAddressToString
    192.0.2.0
    
    0 讨论(0)
  • 2021-01-31 09:26

    This one liner gives you the IP address:

    (Test-Connection -ComputerName $env:computername -count 1).ipv4address.IPAddressToString
    

    Include it in a Variable?

    $IPV4=(Test-Connection -ComputerName $env:computername -count 1).ipv4address.IPAddressToString
    
    0 讨论(0)
提交回复
热议问题