我的收藏夹中有一个.URL文件,如下所示:
[DEFAULT]
BASEURL=http://www.facebook.com/
[{000214A0-0000-0000-C000-000000000046}]
Prop3=19,2
[InternetShortcut]
URL=https://31.13.74.144/
IDList=
IconFile=http://www.facebook.com/favicon.ico
IconIndex=1
我想用我在Powershell脚本中提取的FB的IP地址替换IP地址部分:
# set .url file
$outfile = "C:\Users\aborgetti\Favorites\facebook.url"
# set regex pattern to replace in url file
$regex = '\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b'
# grab the ping
$ping = New-Object System.Net.NetworkInformation.Ping
# grab the facebook IP ONLY
$ips = $($ping.Send("www.facebook.com").Address).IPAddressToString
# output to shell for test
GC $outfile| Where-Object { $_ -match "(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})"}| ForEach-Object {$_ -replace $ips}
我认为我很接近,但我的匹配匹配字符串的整行并返回URL=https://31.13.74.144/
将GC $outfile| Where-Object { $_ -match "(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})"}
的结果存储到变量然后执行子字符串会更容易吗?
我错过了什么吗?我知道必须有一种更简单的方法来做到这一点。
提前致谢!
答案 0 :(得分:2)
任何帮助?
(gc $outfile) -replace '^URL=https://[0-9.]+/',"URL=https://$((Test-Connection www.facebook.com -Count 1 ).IPV4Address)"
答案 1 :(得分:2)
你错了。您正在搜索包含IP的行,然后尝试删除新的IP,即使在文本中也是如此。 -replace 'matchpattern, 'newvalue'
是替换文本的语法。 -replace 'matchpattern'
删除匹配的文字。
您想要做的是:
显示所有行(Get-Content
) - >用新IP替换所有IP。像:
GC $outfile| Foreach-Object { $_ -replace "(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})", $ips}