{"id":4349,"date":"2022-11-12T20:27:52","date_gmt":"2022-11-13T04:27:52","guid":{"rendered":"https:\/\/tungle.ca\/?p=4349"},"modified":"2023-03-13T17:33:41","modified_gmt":"2023-03-14T01:33:41","slug":"query-ip-based-geolocation-via-powershell","status":"publish","type":"post","link":"https:\/\/tungle.ca\/?p=4349","title":{"rendered":"Query IP-based Geolocation via PowerShell"},"content":{"rendered":"\n<p>Using the following site to query IP Geolocation with no API key required (https:\/\/ip-api.com\/).<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"powershell\" class=\"language-powershell\">$IPaddress = read-host \"Enter IP address to check Geo-IP information\"\n$result = Invoke-RestMethod -Method Get -Uri http:\/\/ip-api.com\/json\/$IPaddress\n$result  <\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"708\" height=\"306\" src=\"https:\/\/tungle.ca\/wp-content\/uploads\/2022\/11\/image.png\" alt=\"\" class=\"wp-image-4350\" srcset=\"https:\/\/tungle.ca\/wp-content\/uploads\/2022\/11\/image.png 708w, https:\/\/tungle.ca\/wp-content\/uploads\/2022\/11\/image-300x130.png 300w, https:\/\/tungle.ca\/wp-content\/uploads\/2022\/11\/image-200x86.png 200w\" sizes=\"auto, (max-width: 708px) 100vw, 708px\" \/><\/figure>\n\n\n\n<p>Create a PScustomObject to contain the Geo-IP information that you want to get.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"powershell\" class=\"language-powershell\">$myObject = [PSCustomObject]@{\n            IP = $result.query\n            City = $result.City\n            Country = $result.Country\n            Region = $result.Region\n            ISP = $result.isp\n            }<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"416\" height=\"112\" src=\"https:\/\/tungle.ca\/wp-content\/uploads\/2022\/11\/image-1.png\" alt=\"\" class=\"wp-image-4351\" srcset=\"https:\/\/tungle.ca\/wp-content\/uploads\/2022\/11\/image-1.png 416w, https:\/\/tungle.ca\/wp-content\/uploads\/2022\/11\/image-1-300x81.png 300w, https:\/\/tungle.ca\/wp-content\/uploads\/2022\/11\/image-1-200x54.png 200w\" sizes=\"auto, (max-width: 416px) 100vw, 416px\" \/><\/figure>\n\n\n\n<p>Run a PS code.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"powershell\" class=\"language-powershell\">$myObject | format-list<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"386\" height=\"141\" src=\"https:\/\/tungle.ca\/wp-content\/uploads\/2022\/11\/image-2.png\" alt=\"\" class=\"wp-image-4352\" srcset=\"https:\/\/tungle.ca\/wp-content\/uploads\/2022\/11\/image-2.png 386w, https:\/\/tungle.ca\/wp-content\/uploads\/2022\/11\/image-2-300x110.png 300w, https:\/\/tungle.ca\/wp-content\/uploads\/2022\/11\/image-2-200x73.png 200w\" sizes=\"auto, (max-width: 386px) 100vw, 386px\" \/><\/figure>\n\n\n\n<p>We can covert the output to Json.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"powershell\" class=\"language-powershell\">$myObject | ConvertTo-Json<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"381\" height=\"127\" src=\"https:\/\/tungle.ca\/wp-content\/uploads\/2022\/11\/image-3.png\" alt=\"\" class=\"wp-image-4353\" srcset=\"https:\/\/tungle.ca\/wp-content\/uploads\/2022\/11\/image-3.png 381w, https:\/\/tungle.ca\/wp-content\/uploads\/2022\/11\/image-3-300x100.png 300w, https:\/\/tungle.ca\/wp-content\/uploads\/2022\/11\/image-3-200x67.png 200w\" sizes=\"auto, (max-width: 381px) 100vw, 381px\" \/><\/figure>\n\n\n\n<p>Create a Get-IPaddress function to query the IP-based Geolocation.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"powershell\" class=\"language-powershell line-numbers\">function Get-IPaddress {\n    &lt;#\n    .SYNOPSIS\n        Output the IP-based Geolocation of the given IP address via PowerShell.\n    .DESCRIPTION\n        This PowerShell script outputs the geographic location of the given IP address.\n    .PARAMETER IPAddress\n        Specifies the IP address\n    .EXAMPLE\n        PS> Get-IPaddress 8.8.8.8\n    .INPUTS\n        String\n    .OUTPUTS\n        PSCustomObject\n    .NOTES\n        Author:  Tung\n        Date: 2022-11-15\n    #>\n    [CmdletBinding()]\n    Param (\n        [Parameter(\n            ValueFromPipeline = $true,\n            ValueFromPipelineByPropertyName = $true,\n            Position = 0)]\n     \n        [string[]]\n        $IPaddress = \"\"\n    )\n    BEGIN {}\n    PROCESS {\n        try {\n            if ($IPaddress -eq \"\" ) {\n                $IPaddress = read-host \"Enter IP address to check Geo-IP information\"\n            }\n            $API = @{\n                Uri    = \"http:\/\/ip-api.com\/json\/$IPaddress\"\n                Method = \"Get\"\n            }\n            $result = Invoke-RestMethod @API\n            #$result = Invoke-RestMethod -Method Get -Uri http:\/\/ip-api.com\/json\/$IPaddress\n            # Create a PSCustomObject to contain Geo-IP information\n            $myObject = [PSCustomObject]@{\n                IP      = $result.query\n                City    = $result.City\n                Country = $result.Country\n                Region  = $result.Region\n                ISP     = $result.isp\n            }\n        }\n        catch {\n            #\"Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])\"\n            $_\n        }\n        finally {\n            $myObject | format-list\n            #$myObject | ConvertTo-Json\n        }   \n    }\n}<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"659\" height=\"829\" src=\"https:\/\/tungle.ca\/wp-content\/uploads\/2022\/11\/image-6.png\" alt=\"\" class=\"wp-image-4359\" srcset=\"https:\/\/tungle.ca\/wp-content\/uploads\/2022\/11\/image-6.png 659w, https:\/\/tungle.ca\/wp-content\/uploads\/2022\/11\/image-6-238x300.png 238w, https:\/\/tungle.ca\/wp-content\/uploads\/2022\/11\/image-6-159x200.png 159w\" sizes=\"auto, (max-width: 659px) 100vw, 659px\" \/><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"powershell\" class=\"language-powershell\">Get-IPaddress 8.8.8.8 \nGet-IPaddress 64.59.144.100<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"353\" height=\"154\" src=\"https:\/\/tungle.ca\/wp-content\/uploads\/2022\/11\/image-4.png\" alt=\"\" class=\"wp-image-4354\" srcset=\"https:\/\/tungle.ca\/wp-content\/uploads\/2022\/11\/image-4.png 353w, https:\/\/tungle.ca\/wp-content\/uploads\/2022\/11\/image-4-300x131.png 300w, https:\/\/tungle.ca\/wp-content\/uploads\/2022\/11\/image-4-200x87.png 200w\" sizes=\"auto, (max-width: 353px) 100vw, 353px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/tungle.ca\/wp-content\/uploads\/2022\/11\/image-5.png\" alt=\"\" class=\"wp-image-4355\" width=\"354\" height=\"152\" srcset=\"https:\/\/tungle.ca\/wp-content\/uploads\/2022\/11\/image-5.png 380w, https:\/\/tungle.ca\/wp-content\/uploads\/2022\/11\/image-5-300x129.png 300w, https:\/\/tungle.ca\/wp-content\/uploads\/2022\/11\/image-5-200x86.png 200w\" sizes=\"auto, (max-width: 354px) 100vw, 354px\" \/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>Using the following site to query IP Geolocation with no API key required (https:\/\/ip-api.com\/). Create a PScustomObject to contain the Geo-IP information that you want to get. Run a PS code. We can covert the output to Json. Create a Get-IPaddress function to query the IP-based Geolocation.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9,7],"tags":[10,12],"class_list":["post-4349","post","type-post","status-publish","format-standard","hentry","category-microsoft","category-powershell","tag-microsoft","tag-powershell"],"_links":{"self":[{"href":"https:\/\/tungle.ca\/index.php?rest_route=\/wp\/v2\/posts\/4349","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/tungle.ca\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tungle.ca\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tungle.ca\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/tungle.ca\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4349"}],"version-history":[{"count":8,"href":"https:\/\/tungle.ca\/index.php?rest_route=\/wp\/v2\/posts\/4349\/revisions"}],"predecessor-version":[{"id":4497,"href":"https:\/\/tungle.ca\/index.php?rest_route=\/wp\/v2\/posts\/4349\/revisions\/4497"}],"wp:attachment":[{"href":"https:\/\/tungle.ca\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4349"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tungle.ca\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4349"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tungle.ca\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4349"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}