Use:
nslookup myip.opendns.com. resolver1.opendns.com
from the commando prompt or via a script.
It will return something like:
Server: resolver1.opendns.com
Address: 208.67.222.222
Non-authoritative answer:
Name: myip.opendns.com
Address: 12.34.56.78
Where 12.34.56.78 will be your External IP address.
Thanks to http://superuser.com/questions/165986/windows-command-that-returns-external-ip