Wednesday 11 March 2009

Get-CountryForIPAddress.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     Gets country for a Geo-IP address 
  4. .DESCRIPTION 
  5.     This function uses the New-WebServiceProxy cmdlet to 
  6.     create a web service proxy. Then it calls that proxy 
  7.     to get the country for a given IP address. After the 
  8.     function definition are two examples! 
  9. .NOTES 
  10.     File Name  : Get-CountryForIPAddress.ps1 
  11.     Author     : Thomas Lee - tfl@psp.co.uk 
  12.     Requires   : PowerShell V2 CTP3 
  13. .LINK 
  14.    Script posted: 
  15.    http://www.pshscripts.blogspot.com 
  16. .INPUTTYPE 
  17.    String representing IP Adress 
  18. .RETURNVALUE 
  19.    XML Element, holding details of the country 
  20. .EXAMPLE 
  21.     Run from PowerShell Prompt: 
  22.     Looking up: 131.107.2.200 
  23.     Country: UNITED STATES (US) 
  24. .EXAMPLE 
  25.     Run from PipelineL 
  26.     "16.0.0.1","200.200.200.1" | Get-CountryForIPAddress 
  27.     Looking up: 16.0.0.1 
  28.     Country: UNITED STATES (US) 
  29.     Looking up: 200.200.200.1 
  30.     Country: BRAZIL (BR) 
  31. .PARAMETER IPAddress 
  32.     A string, or array of strings representing IP Addresses 
  33.     The function gets country details for each IPAddress provided 
  34. #> 
  35.  
  36. function Get-CountryForIPAddress { 
  37. param
  38. [Parameter(Position=0, Mandatory=$FALSE, ValueFromPipeline=$TRUE)]  
  39. [String] $IPAddress="131.107.2.200"
  40. process { 
  41.    "Looking up: {0}" -f $IPAddress 
  42.    $s = new-webserviceproxy -uri http://www.webservicex.net/geoipservice.asmx 
  43.    foreach ($addr in $IPAddress) { 
  44.       $result = $s.GetGeoIP($addr
  45.       "Country: {0} ({1})" -f $result.countryname, $result.countrycode 
  46.     } # end foreach 
  47. } #end process block 
  48. } # end function 
  49.  
  50.  
  51. "Example 1:" 
  52. "==========" 
  53. Get-CountryForIPAddress "131.107.2.200" 
  54.  
  55. "Example 2:" 
  56. "==========" 
  57. "16.0.0.1","200.200.200.1" | Get-CountryForIPAddress 
  58. # End of Script 

No comments: