Powershell – Get-CountryForIPAddress function

Just a little looping script to take an IP address and return the country and country code.

function Get-CountryForIPAddress {
    param(
    [Parameter(Position=0, Mandatory=$FALSE, ValueFromPipeline=$TRUE)]
    [String] $IPAddress="10.0.0.1" )
    process {
        "Looking up: {0}" -f $IPAddress
        $s = new-webserviceproxy -uri http://www.webservicex.net/geoipservice.asmx 
        foreach ($addr in $IPAddress) {
            $result = $s.GetGeoIP($addr)
            "Country: {0} ({1})" -f $result.countryname, $result.countrycode
        }
    }
}
while ($IPAdd -ne "exit") {
    Write-Host "Enter IP Address ('exit' to finish):" -ForegroundColor Green
    $IPAdd = read-host
    if ($IPAdd -ne "exit") {
        $a = [string] $IPAdd
        $b = $a.split(",")
        foreach ($ip in $b) {
            Get-CountryForIPAddress $ip
        }
    }
}

Copy the code block, and paste into your text editor. Save as Get-CountryForIPAddress.ps1 and then run in Powershell. Done!

Advertisements

About Cameron
I'm a final year Computer Science/Information Systems major. Already finished my BA in Politics/Philosophy. I do web and software freelance on the side, while I finish studying. Hoping to be self-employed by the end of my degree, otherwise off into the real-world I go....

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: