I have a script that I use to check if a domain has dns records A,MX,SPF and DMARC.
I recently ran into a problem were if I sent the script this example test.domain.com
. The function will test that as is.
I really want the script to only test the domain.com
portion unless told otherwise with a switch. (I know how to build a switch)
The problem is I don't know how to test for if a flat domain was entered versus a domain that has a subdomain like my example above. I would like to get a result of just flat domain.
ideas?
here is my function
param (
[parameter(Mandatory = $true,
HelpMessage = "Enter the domain name or a email address.")][string]$Domain
)
If ($Domain -notlike '*.*') {
Write-Warning "Domain not valid please input full domain. Example Facebook.com or an email address."
Return $null
}
Try {
$TestDomain = ([Net.Mail.MailAddress]$Domain).Host
}
Catch {
$TestDomain = $Domain
}
If ($TestDomain -like '@*') {
$TestDomain = $TestDomain.Replace('@', '')
}
Return [PSCustomObject]@{
A = If (Resolve-DnsName -Name $TestDomain -Type 'A' -Server '8.8.8.8' -DnsOnly -ErrorAction SilentlyContinue | Where-Object { $_.type -eq 'a' } ) { $true } Else { $false }
MX = If (Resolve-DnsName -Name $TestDomain -Type 'MX' -Server '8.8.8.8' -DnsOnly -ErrorAction SilentlyContinue | Where-Object { $_.type -eq 'mx' } ) { $true } Else { $false }
SPF = If (Resolve-DnsName -Name $TestDomain -Type 'TXT'-Server '8.8.8.8' -DnsOnly -ErrorAction SilentlyContinue | where-object { $_.strings -match "v=spf1" } ) { $true } Else { $false }
DMARC = if (Resolve-DnsName -Name "_dmarc.$($TestDomain)" -Type 'TXT' -Server '8.8.8.8' -DnsOnly -ErrorAction SilentlyContinue | Where-Object { $_.type -eq 'txt' } ) { $true } Else { $false }
}
CodePudding user response:
I don't know exactly what you are trying to do. I have a general idea. In order to check for a subdomain I would just use a regular expression to check for one or more '.'. This could be accomplished like so:
if($Domain -match '.*\..*\..*') {#Subdomain}
Here is a more detailed example using your current code and how you could apply it:
param (
[parameter(Mandatory = $true,
HelpMessage = "Enter the domain name or a email address.")][string]$Domain
)
#two or more "." -subdomain
If($Domain -match '.*\..*\..*')
{
#example: photos.google.com
#1). if you want to take off the sub domain off of the $domain variable:
$Domain = $Domain.Substring($Domain.IndexOf('.') 1)
#2). or exit out
Write-Warning "Domain entered is a subdomain... Exiting"
return $null
#3). or use the "subdomain" a different way than a "root" domain
}
#match one ore more "." -root
elseif ($Domain -match '.*\..*')
{
#example: google.com
#1). Execute what you want to do with Domain here --- if doing something seperate above ... using option 2 / 3 from above condition
#2). no need to execute anything here if you are using option 1 from above
}
#invalid
else
{
Write-Warning "Domain not valid please input full domain. Example Facebook.com or an email address."
Return $null
}
#work with the $Domain variable ... Option 1 / 2 on the first condition.
In the example above you could put your Try{} Catch{}
block in the appropriate condition or after the conditions. It is really up to you how you want to handle it.
CodePudding user response:
Here is how this worked out for me. I wanted a simple script to tell me if a domain like facebook had certain dns records. I also found that sometimes I want the subdomain so this is what I have so far.
param (
[parameter(Mandatory = $true,
HelpMessage = "Enter the domain name or a email address.")][string]$Domain,
[parameter(Mandatory = $false,
HelpMessage = "Allow subdomain")][switch]$Sub
)
If ($Domain -notlike '*.*') {
Write-Warning "Domain not valid please input full domain. Example Facebook.com or an email address."
Return $null
}
Try {
$TestDomain = ([Net.Mail.MailAddress]$Domain).Host
}
Catch {
$TestDomain = $Domain
}
If ($TestDomain -like '@*') {
$TestDomain = $TestDomain.Replace('@', '')
}
if (-not $Sub) {
$TestDomain = $TestDomain.Split(".")[-2, -1] -join "."
}
Return [PSCustomObject]@{
A = If (Resolve-DnsName -Name $TestDomain -Type 'A' -Server '8.8.8.8' -DnsOnly -ErrorAction SilentlyContinue | Where-Object { $_.type -eq 'a' } ) { $true } Else { $false }
MX = If (Resolve-DnsName -Name $TestDomain -Type 'MX' -Server '8.8.8.8' -DnsOnly -ErrorAction SilentlyContinue | Where-Object { $_.type -eq 'mx' } ) { $true } Else { $false }
SPF = If (Resolve-DnsName -Name $TestDomain -Type 'TXT'-Server '8.8.8.8' -DnsOnly -ErrorAction SilentlyContinue | where-object { $_.strings -match "v=spf1" } ) { $true } Else { $false }
DMARC = if (Resolve-DnsName -Name "_dmarc.$($TestDomain)" -Type 'TXT' -Server '8.8.8.8' -DnsOnly -ErrorAction SilentlyContinue | Where-Object { $_.type -eq 'txt' } ) { $true } Else { $false }
DOMAIN = $TestDomain
}