Getting IPv4 address and using it as an argument for netsh function

Hi there!

About a week ago I decided to set up the IPv6 connection via tunnelbroker but my ISP gives only dynamic dedicated IPv4 addresses. The tunnelbroker gives a solution to update the dynamic IPv4 address but it works only on server side. It means that every time my IPv4 address changes, I need to delete the old tunnel and create a new one.
I’ve found a solution, the Powershsell script
http://josherickson.org/132/hurricane-electric-6in4-windows-startup-script/
function fastpingtest {
$ping = New-Object System.Net.NetworkInformation.Ping;
$ping.Send(“8.8.8.8”, 1000).status -eq “success”;
}
$endtime = [datetime]::Now.AddMinutes(1);
$mapipv6 = $false;
while([datetime]::Now -lt $endtime) {
if(fastpingtest) { $mapipv6 = $true; break; }
}

if($mapipv6) {
$wc = New-Object net.webclient;
$url= “https://ipv4.tunnelbroker.net/ipv4_end.php?ip=AUTO&pass={1}&apikey={0}&tid={2}”;

$values = "USERID", "PASSWORDMD5HASH", TUNNELID;
$wc.DownloadString(($url -f $values));

#get connected interface
$interface = netsh interface ipv4 show interface | findstr /c:" connected" | ?{!$_.contains("Loopback");} | %{[regex]::Split($_, "( )+") | ?{$_.trim().length -gt 0} | %{$_.trim()}; }
$interface_ip = (netsh interface ipv4 show address $interface[0] | findstr /c:"IP Address" | select -First 1).split(":")[1].trim()
netsh interface teredo set state disabled
netsh interface ipv6 add v6v4tunnel IP6Tunnel $interface_ip HEIPv4ENDPOINT
netsh interface ipv6 add address IP6Tunnel YOURIPv6ADDRESS
netsh interface ipv6 add route ::/0 IP6Tunnel HEIPv6ADDRESS</code>

But it didn’t work - the problem starts from #get connected interface
comment. I contacted the Author and he adviced me to replace !$.contains(“Loopback”); with $.contains(“Satel”); (name of my Internet connection) and also I decided to replace findstr /c:“IP Address” with findstr /c:“IP-Адрес” (because I have a russian version of Windows 7).
Anyway the problem is that $interface_ip gets wring value and netsh command gives an error:
-ERROR: Invalid IPv4 address supplied

Then I’ve found another way to get the IPv4 address:
#get connected interface
$ipAddress = Test-Connection -ComputerName EAGauss -Count 1 | Select -ExpandProperty IPV4Address
$interface_ip = $ipAddress.IPAddressToString

Where EAGauss - is my hostname. It worked separately from the code but when I tried to change old strings to the new ones - it gave the same error.
-ERROR: Invalid IPv4 address supplied

I’m waiting for your help.
Regards,
EAGauss

This is a duplicate of http://powershell.com/cs/forums/t/23607.aspx

Need to see what data you are getting in your variables. You say that $interface_ip has the wrong value, but what do you mean by wrong? Is it getting an IP just off the wrong interface?, is it blank?, does it have some other data there?