在PowerShell 3.0 Windows 7中是否有一种简单的方法可以将本地计算机的ipv4地址转换为变量?
这是另一个解决方案:
$env:HostIP = (
Get-NetIPConfiguration |
Where-Object {
$_.IPv4DefaultGateway -ne $null -and
$_.NetAdapter.Status -ne "Disconnected"
}
).IPv4Address.IPAddress
获取设备的IPv4地址,并过滤以仅获取与您的方案匹配的地址(即忽略和APIPA地址或LocalHost地址)。你可以说抓住匹配192.168.200.*
的地址。
$IPv4Addr = Get-NetIPAddress -AddressFamily ipV4 | where {$_.IPAddress -like X.X.X.X} | Select IPAddress
这个怎么样? (不是我的真实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
如果我使用机器名称,这是有效的。但有点像黑客(因为我只是选择了我获得的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]
(Get-WmiObject -Class Win32_NetworkAdapterConfiguration | where {$_.DefaultIPGateway -ne $null}).IPAddress | select-object -first 1
这是我最终使用的
$ipaddress = $(ipconfig | where {$_ -match 'IPv4.+\s(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})' } | out-null; $Matches[1])
它被打破了
使用$env
环境变量获取主机名的另一个变体:
Test-Connection -ComputerName $env:computername -count 1 | Select-Object IPV4Address
或者,如果您只想要返回没有属性标头的IP地址
(Test-Connection -ComputerName $env:computername -count 1).IPV4Address.ipaddressTOstring
这一个班轮为您提供IP地址:
(Test-Connection -ComputerName $env:computername -count 1).ipv4address.IPAddressToString
将它包含在变量中?
$IPV4=(Test-Connection -ComputerName $env:computername -count 1).ipv4address.IPAddressToString
我最近遇到了同样的问题。所以我写了一个脚本来从ipconfig /all
输出中解析它。此脚本可以轻松修改以获取接口的任何参数,它也可以在Windows 7上运行。
LineNumber | Line
格式获取IP配置的输出$ip_config = $(ipconfig /all | % {$_ -split "
rn"} | Select-String -Pattern ".*" | select LineNumber, Line)
ipconfig
格式获取接口列表(+ LineNumber | Line
输出的最后一行)$interfaces = $($ip_config | where {$_.Line -notmatch '^\s*$'} | where {$_.Line -notmatch '^\s'}) + $($ip_config | Select -last 1)
$LAN = $($interfaces | where {$_.Line -match 'Wireless Network Connection:$'})
$i = $interfaces.IndexOf($LAN)
$start = $LAN.LineNumber
$end = $interfaces[$i+1].LineNumber
start..end
的线路$LAN = $ip_config | where {$_.LineNumber -in ($start..$end)}
$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}}
$a = ipconfig
$result = $a[8] -replace "IPv4 Address. . . . . . . . . . . :",""
还要检查ipconfig的哪个索引具有IPv4地址