-
Notifications
You must be signed in to change notification settings - Fork 74
/
Copy pathGet-GeoIP.ps1
142 lines (137 loc) · 10.2 KB
/
Get-GeoIP.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
Function Get-GeoIP {
<#
.SYNOPSIS
Display geographical location for a given IP address. Can also view your own ISP given address.
.DESCRIPTION
Display geographical location for a given IP address. Can also view your own ISP given address. There is a possibility for this to fail if the web service being used is unavailable.
.PARAMETER Ip
IP address to find country to origin
.PARAMETER ShowInternetIP
Gets your internet IP address and country of origin
.PARAMETER Credential
Use alternate credentials
.PARAMETER UseDefaultCredential
Use default credentials
.NOTES
Name: Get-GeoIP
Author: Boe Prox
DateCreated: 16Feb2011
.LINK
http://www.webservicex.net/ws/default.aspx
.LINK
http://boeprox.wordpress.com
.EXAMPLE
Get-GeoIP -IP 192.168.1.1
Description
-----------
Returns the country of origin for the specified IP Address. In this case, United States
.EXAMPLE
Get-GeoIP -ShowInternetIP
Description
-----------
Returns the Internet address from where this command was run.
#>
[cmdletbinding(
DefaultParameterSetName = 'Default',
ConfirmImpact = 'low'
)]
Param(
[Parameter(
Mandatory = $False,
Position = 0,
ParameterSetName = '',
ValueFromPipeline = $True)]
[string]$Ip,
[Parameter(
Position = 1,
Mandatory = $False,
ParameterSetName = '')]
[switch]$ShowInternetIP,
[Parameter(
Position = 2,
Mandatory = $False,
ParameterSetName = 'DefaultCred')]
[switch]$UseDefaultCredental,
[Parameter(
Position = 3,
Mandatory = $False,
ParameterSetName = 'AltCred')]
[System.Management.Automation.PSCredential]$Credential
)
Begin {
$psBoundParameters.GetEnumerator() | % {
Write-Verbose "Parameter: $_"
}
#Ensure that user is not using both -City and -ListCities parameters
Write-Verbose "Verifying that both City and ListCities is not being used in same command."
If ($PSBoundParameters.ContainsKey('ListCities') -AND $PSBoundParameters.ContainsKey('City')) {
Write-Warning "You cannot use both -City and -ListCities in the same command!"
Break
}
Switch ($PSCmdlet.ParameterSetName) {
AltCred {
Try {
#Make connection to known good geo ip service using DefaultCredentials
Write-Verbose "Create web proxy connection to geo ip service using Alternate Credentials"
$geoip = New-WebServiceProxy 'http://www.webservicex.net/geoipservice.asmx?WSDL' -Credential $credential
}
Catch {
Write-Warning "$($Error[0])"
Break
}
}
DefaultCred {
Try {
#Make connection to known good geo ip service using Alternate Credentials
Write-Verbose "Create web proxy connection to geo ip service using DefaultCredentials"
$geoip = New-WebServiceProxy 'http://www.webservicex.net/geoipservice.asmx?WSDL' -UseDefaultCredential
}
Catch {
Write-Warning "$($Error[0])"
Break
}
}
Default {
Try {
#Make connection to known good geo ip service
Write-Verbose "Create web proxy connection to geo ip service"
$geoip = New-WebServiceProxy 'http://www.webservicex.net/geoipservice.asmx?WSDL'
}
Catch {
Write-Warning "$($Error[0])"
Break
}
}
}
}
Process {
#Determine if we are only to list the cities for a given country or get the weather from a city
If ($PSBoundParameters.ContainsKey('Ip')) {
Try {
#List all cities available to query for geo ip
Write-Verbose "Retrieving location of IP: $($ip)"
$geoip.GetGeoIP($ip)
Break
}
Catch {
Write-Warning "$($Error[0])"
Break
}
}
If ($PSBoundParameters.ContainsKey('ShowInternetIP')) {
Try {
#Get your Internet IP and geo location
Write-Verbose "Retrieving internet IP and location"
$geoip.GetGeoIPContext()
Break
}
Catch {
Write-Warning "$($Error[0])"
Break
}
}
}
End {
Write-Verbose "End function"
}
}