Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

dealing with subdomain and domain

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)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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 }
}

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading