-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGet-Domain.ps1
39 lines (36 loc) · 891 Bytes
/
Get-Domain.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
function Get-Domain
{
[CmdletBinding()]
param
(
[Parameter(Mandatory, Position=0)]
[String]
$Fqdn
)
# Create TLDs List as save it to "script" for faster next run.
if (!$TldsList)
{
$TldsListRow = Invoke-RestMethod -Uri https://publicsuffix.org/list/public_suffix_list.dat
$script:TldsList = ($TldsListRow -split "`n" | Where-Object {$_ -notlike '//*' -and $_})
[array]::Reverse($TldsList)
}
#Remove-Variable TldsList
$Ok = $false
foreach ($Tld in $TldsList)
{
if ($Fqdn -Like "*.$Tld")
{
$Ok = $true
break
}
}
#$Tld = $TldList | Where-Object {$Domain -Like "*.$_"} | Select-Object -Last 1
if ($Ok)
{
($Fqdn -replace "\.$Tld" -split '\.')[-1] + ".$Tld"
}
else
{
throw 'Not a valid TLD'
}
}