WebServiceX offers a public Web Service which allows you to enter an IP and returns the country where that IP is registered.
 
In a WinForms Form I added the Input and Output TextBoxes.

Add-Type -AssemblyName System.Windows.Forms

# Main Form
$mainForm = New-Object System.Windows.Forms.Form
$mainForm.AcceptButton = $lookupButton
$mainForm.Font = "Comic Sans MS,10"
$mainForm.Text = " GeoIP - Country Lookup by IP"
$mainForm.ForeColor = "White"
$mainForm.BackColor = "DarkGray"
$mainForm.Width = 600
$mainForm.Height = 90

# Input TextBox
$inputTextBox = New-Object System.Windows.Forms.TextBox
$inputTextBox.Location = "45,10"
$inputTextBox.Size = "150,20"
$inputTextBox.ForeColor = "MediumBlue"
$inputTextBox.BackColor = "White"
$inputTextBox.Text = "Enter IP"
$mainForm.Controls.Add($inputTextBox)

# OutputTextBox
$outputTextBox = New-Object System.Windows.Forms.TextBox
$outputTextBox.Location = "200,10"
$outputTextBox.Size = "185,20"
$outputTextBox.ForeColor = "MediumBlue"
$outputTextBox.BackColor = "White"
$mainForm.Controls.Add($outputTextBox)

 
When you click the “Lookup” Button the GeoIPLookup Function is called. In that Function I clear the Form and reset the error checking. Using New-WebServiceProxy I make a SOAP Request of GetGeoIP to WebServiceX.net passing the collected IP.

# Lookup Button
$lookupButton = New-Object System.Windows.Forms.Button
$lookupButton.Location = "395,10"
$lookupButton.Size = "75,28"
$lookupButton.ForeColor = "DarkBlue"
$lookupButton.BackColor = "White"
$lookupButton.Text = "Lookup"
$lookupButton.add_Click({GeoIPLookup})
$mainForm.Controls.Add($lookupButton)

Function GeoIPLookup {
    $outputTextBox.Text = " "
    $mainForm.Refresh()
    $error.clear()
    $GeoIP = New-WebServiceProxy -uri "http://www.webservicex.net/geoipservice.asmx?WSDL"
    $GeoIPArray = $GeoIP.GetGeoIP($inputTextBox.Text)

 
I check for errors from the returned value and pass back the Country Name if the ReturnCode is 1 else the Results Code. If I get an error from the call I present a message.

    If ($GeoIPArray.ReturnCode -eq "1") {
        $outputTextBox.Text = $GeoIPArray.CountryName
    }
    Else {
        $outputTextBox.Text = $GeoIPArray.ReturnCodeDetails
    }
    If($error.count -gt 0) {
        $outputTextBox.Text = "Bad Lookup"
    }
}

# Example 0f Successful Return from WebService -
# ReturnCode        : 1
# IP                : 25.199.80.1
# ReturnCodeDetails : Success
# CountryName       : United Kingdom
# CountryCode       : GBR
#
# Sending Bad IP Returns -
# ReturnCode        : 0
# IP                : 25199.80.1
# ReturnCodeDetails : Invalid IP address
# CountryName       :
# CountryCode       :

 
I add and Exit Button and diaplay the Form.

# Exit Button
$exitButton = New-Object System.Windows.Forms.Button
$exitButton.Location = "470,10"
$exitButton.Size = "75,28"
$exitButton.ForeColor = "Red"
$exitButton.BackColor = "White"
$exitButton.Text = "Exit"
$exitButton.add_Click({$mainForm.close()})
$mainForm.Controls.Add($exitButton)

[void] $mainForm.ShowDialog()

 
GeoIP
 
GeoIP.ps1

Advertisements