Tuesday, September 27, 2016

PowerShell v3 Get IP from HostName

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:
([System.Net.Dns]::GetHostAddresses(www.bing.com)[0]).IPAddressToString;
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:
[System.Net.Dns].GetMethod(GetHostAddresses)


Name                       : GetHostAddresses
DeclaringType              : System.Net.Dns
ReflectedType              : System.Net.Dns
MemberType                 : Method
MetadataToken              : 100671134
Module                     : System.dll
IsSecurityCritical         : True
IsSecuritySafeCritical     : True
IsSecurityTransparent      : False
MethodHandle               : System.RuntimeMethodHandle
Attributes                 : PrivateScope, Public, Static, HideBySig
CallingConvention          : Standard
ReturnType                 : System.Net.IPAddress[]
ReturnTypeCustomAttributes : System.Net.IPAddress[]
ReturnParameter            : System.Net.IPAddress[]
IsGenericMethod            : False
IsGenericMethodDefinition  : False
ContainsGenericParameters  : False
IsPublic                   : True
IsPrivate                  : False
IsFamily                   : False
IsAssembly                 : False
IsFamilyAndAssembly        : False
IsFamilyOrAssembly         : False
IsStatic                   : True
IsFinal                    : False
IsVirtual                  : False
IsHideBySig                : True
IsAbstract                 : False
IsSpecialName              : False
IsConstructor              : False
And, accepts the following parameters:
[System.Net.Dns].GetMethod(GetHostAddresses).GetParameters()


ParameterType   : System.String
Name            : hostNameOrAddress
DefaultValue    :
RawDefaultValue :
MetadataToken   : 134226623
Position        : 0
Attributes      : None
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. 

Related Post:

0 comments:

Post a Comment

 
Copyright 2009 Information Blog
Powered By Blogger