-
Notifications
You must be signed in to change notification settings - Fork 14
/
Get-PublicIP.ps1
65 lines (46 loc) · 1.76 KB
/
Get-PublicIP.ps1
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
Function Get-PublicIp {
<#
.SYNOPSIS
This cmdlet is used to get the public ip address of the local device.
.DESCRIPTION
Get-PublicIp gets the public IP address of the local machine and displays other info as well such as Provider, City, etc.
This is done thanks to an API at https://ipinfo.io/json
.NOTES
Author: Robert H. Osborne
Alias: tobor
Contact: [email protected]
.LINK
https://osbornepro.com
https://writeups.osbornepro.com
https://btpssecpack.osbornepro.com
https://github.com/tobor88
https://gitlab.com/tobor88
https://www.powershellgallery.com/profiles/tobor
https://www.linkedin.com/in/roberthosborne/
https://www.credly.com/users/roberthosborne/badges
https://www.hackthebox.eu/profile/52286
.EXAMPLE
----------------- EXAMPLE 1 --------------------
PS> Get-PublicIP
.INPUTS
None
.OUTPUTS
PSCustomObject
#>
[CmdletBinding()]
param()
$UserAgent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:104.0) Gecko/20100101 Firefox/104.0'
$IpInfo = Invoke-RestMethod -Uri https://ipinfo.io/json -UseBasicParsing -Method GET -UserAgent $UserAgent
If ($Null -like $IpInfo) {
Throw " [x] Could not connect to API at http://ipinfo.io/json. Check internet connection and site availability."
} # End Else
$Obj = New-Object -TypeName "PsObject" -Property @{IPv4 = $IpInfo.Ip
Hostname = $IpInfo.Hostname
City = $IpInfo.City
Region = $IpInfo.Region
Country = $IpInfo.Country
GeoLoc = $IpInfo.Loc
Organization = $IpInfo.Org
} # End Properties
$Obj
} # End Function