Get-Geo PowerShell function will resolve the IP address of a domain and return the country name and country code information:
1 2 3 4 5 |
PS C:\> Get-Geo yandex.ru Domain Name IP Address Country Name Country Code ----------- ---------- ------------ ------------ yandex.ru 213.180.204.11 Russian Federation RUS |
Add this function to a module or save it as ps1 file and dot source it:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
<# .SYNOPSIS Get-Geo - Domain/Hostname Geolocation .DESCRIPTION Resolves the IP address of a domain and returns the country name and country code information. .PARAMETER domain Specifies the domain name (enter the domain name without http:// and www (e.g. power-shell.com)) .EXAMPLE Get-Geo -domain microsoft.com Get-Geo microsoft.com .NOTES File Name: get-geo.ps1 Author: Nikolay Petkov Blog: http://77.104.138.174/~powershe/power-shell.com Last Edit: 12/20/2014 .LINK http://77.104.138.174/~powershe/power-shell.com #> Function Get-Geo { param ( [Parameter(Mandatory=$True, HelpMessage='Please enter domain name (e.g. microsoft.com)')] [string]$domain ) try { $ipadresses = [System.Net.Dns]::GetHostAddresses($domain) |where {$_.AddressFamily -eq "InterNetwork"} ` |foreach {$_.IPAddressToString} |Out-String $ip = $ipadresses.Split()[0] } catch { Write-Host "Get-Geo request could not find host $domain. Please check the name and try again." -ForegroundColor Red } $geo = New-WebServiceProxy -uri "http://www.webservicex.net/geoipservice.asmx?WSDL" $results = $geo.GetGeoIP($ip) If ($results.ReturnCodeDetails -eq "Success") { $GeoObject = New-Object PSObject Add-Member -inputObject $GeoObject -memberType NoteProperty -name "Domain Name" -value $domain Add-Member -inputObject $GeoObject -memberType NoteProperty -name "IP Address" -value $results.IP Add-Member -inputObject $GeoObject -memberType NoteProperty -name "Country" -value $results.CountryName Add-Member -inputObject $GeoObject -memberType NoteProperty -name "Country Code" -value $results.CountryCode $GeoObject } else {Write-Host "The $domain GEO location cannot be resolved." -ForegroundColor Red} } #end function Get-Geo |