This is a topic lots of folks have covered. I just revisited it and thought I would jot it down for posteritys sake. Note, I took the basic C# from this link:
Get IP Address from DNS Hostname in C#
Specifically, I just needed to know what the IPv4 address was, so, I used this approach:
Since there may be more than one IP Address, you need to deal with it as is appropriate in your case. Here, I just needed 1 address I could use for testing a public internet IP address. If you want to back up a step and look at the broader result set from the query, here is what you will get:
[System.Net.Dns]::GetHostAddresses(www.bing.com)
Address : 601988312
AddressFamily : InterNetwork
ScopeId :
IsIPv6Multicast : False
IsIPv6LinkLocal : False
IsIPv6SiteLocal : False
IsIPv6Teredo : False
IPAddressToString : 216.156.225.35
Address : 551656664
AddressFamily : InterNetwork
ScopeId :
IsIPv6Multicast : False
IsIPv6LinkLocal : False
IsIPv6SiteLocal : False
IsIPv6Teredo : False
IPAddressToString : 216.156.225.32
In this case, I simply took the lowest IP address (array index 0) and grabbed the IPAddressToString property. The method, .GetHostAddresses, has the following definition:
Member : System.Net.IPAddress[] GetHostAddresses(System.String)
IsIn : False
IsOut : False
IsLcid : False
IsRetval : False
IsOptional : False
This quick approach allows you to drop this one line call into scripts and get IP addresses you can use quickly and easily.
About lamsim
Author Description here.. Nulla sagittis convallis. Curabitur consequat. Quisque metus enim, venenatis fermentum, mollis in, porta et, nibh. Duis vulputate elit in elit. Mauris dictum libero id justo.