Reputation: 2860
I have a .URL file in my favorites like this:
[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
I want to replace the IP address portion with the IP address of FB which I pull out in a Powershell script:
# 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}
I think I'm close, but my -match matches the entire line of the string and returns URL=https://31.13.74.144/
Would it be easier to store the results of GC $outfile| Where-Object { $_ -match "(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})"}
to a variable and then do a substring?
Am I missing something? I know there's got to be an easier way to do this.
Thanks in advance!
Upvotes: 0
Views: 1966
Reputation: 54911
You're using it wrong. You're searching for lines including an IP, and then trying to remove the new ip, which wasn't even in the text. -replace 'matchpattern, 'newvalue'
is the syntax to replace text. -replace 'matchpattern'
deletes the matched text.
What you want to do is:
Show all lines(Get-Content
) -> Replace all IPs with new IP. Like:
GC $outfile| Foreach-Object { $_ -replace "(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})", $ips}
Upvotes: 2
Reputation: 68321
Any help?
(gc $outfile) -replace '^URL=https://[0-9.]+/',"URL=https://$((Test-Connection www.facebook.com -Count 1 ).IPV4Address)"
Upvotes: 2