Powershell将ipv4地址转换为变量

问题描述 投票:23回答:10

在PowerShell 3.0 Windows 7中是否有一种简单的方法可以将本地计算机的ipv4地址转换为变量?

windows powershell-v3.0
10个回答
32
投票

这是另一个解决方案:

$env:HostIP = (
    Get-NetIPConfiguration |
    Where-Object {
        $_.IPv4DefaultGateway -ne $null -and
        $_.NetAdapter.Status -ne "Disconnected"
    }
).IPv4Address.IPAddress

-1
投票

获取设备的IPv4地址,并过滤以仅获取与您的方案匹配的地址(即忽略和APIPA地址或LocalHost地址)。你可以说抓住匹配192.168.200.*的地址。

$IPv4Addr = Get-NetIPAddress -AddressFamily ipV4 | where {$_.IPAddress -like X.X.X.X} | Select IPAddress

25
投票

这个怎么样? (不是我的真实IP地址!)

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

PS C:\> $ipV4

IPV4Address                                                  
-----------
192.0.2.0

请注意,使用localhost只会返回并且IP为127.0.0.1

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

PS C:\> $ipV4

IPV4Address                                                             
-----------                                                  
127.0.0.1

必须扩展IP地址对象以获取地址字符串

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

10
投票

如果我使用机器名称,这是有效的。但有点像黑客(因为我只是选择了我获得的ipv4地址的第一个值。)

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

请注意,您必须在上面的表达式中替换值PasteMachineNameHere

这也有效

$localIpAddress=((ipconfig | findstr [0-9].\.)[0]).Split()[-1]

8
投票
(Get-WmiObject -Class Win32_NetworkAdapterConfiguration | where {$_.DefaultIPGateway -ne $null}).IPAddress | select-object -first 1

6
投票

这是我最终使用的

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

它被打破了

  • execute ipconfig命令 - 获取所有网络接口信息
  • 使用powershell,其中filter带有正则表达式
  • 正则表达式查找具有“IPv4”的行和一组4个块,每个块具有由句点分隔的1-3个数字,即v4 IP地址
  • 通过将输出管道为null来忽略输出
  • 最后得到正则表达式括号中定义的第一个匹配组。
  • 捕获$ ipaddress中的输出以供以后使用。

3
投票

使用$env环境变量获取主机名的另一个变体:

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

或者,如果您只想要返回没有属性标头的IP地址

(Test-Connection -ComputerName $env:computername -count 1).IPV4Address.ipaddressTOstring

2
投票

这一个班轮为您提供IP地址:

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

将它包含在变量中?

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

-1
投票

我最近遇到了同样的问题。所以我写了一个脚本来从ipconfig /all输出中解析它。此脚本可以轻松修改以获取接口的任何参数,它也可以在Windows 7上运行。

  1. LineNumber | Line格式获取IP配置的输出

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

  1. ipconfig格式获取接口列表(+ LineNumber | Line输出的最后一行)

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

  1. 通过接口列表筛选所需的特定接口

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

  1. 从输出中获取所选接口的起始和结束行号

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

  1. 选择start..end的线路

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

  1. 获取IP(v4)地址字段(如果不存在IPv4地址,则返回null)

$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}}


-1
投票
$a = ipconfig
$result = $a[8] -replace "IPv4 Address. . . . . . . . . . . :",""

还要检查ipconfig的哪个索引具有IPv4地址

© www.soinside.com 2019 - 2024. All rights reserved.