0
param (    
    [parameter(Mandatory = $true,
        HelpMessage = "Enter the full domain name. Example Facebook.com or enter an entire email address.")]
    [ValidateScript({
            if ($_ -like "*.*") {
                return $true
            }
            else {
                Throw [System.Management.Automation.ValidationMetadataException] "Enter a domain name like facebook.com or an entire email address."
            }
        })][string]$Domain
)

#if email address pull down to domain and if not test domain
$TestDomain = $null
Try {
    $TestDomain = ([Net.Mail.MailAddress]$Domain).Host
}
Catch {
    [string]$TestDomain = $Domain
}

[string]$resultA = If (Resolve-DnsName -Name $TestDomain -Type 'A' -Server '8.8.8.8' -DnsOnly -ErrorAction SilentlyContinue | Where-Object { $_.type -eq 'a' } ) { $true } Else { $false }$mx = Resolve-DnsName -Name $TestDomain -Type 'MX' -Server '8.8.8.8' -DnsOnly -ErrorAction SilentlyContinue | Sort-Object -Property Preference -ErrorAction SilentlyContinue
if ([string]::IsNullOrWhiteSpace($mx.NameExchange)) {
    [string]$resultmx = $false
}
Else {
    [array]$resultmx = $mx | ForEach-Object {
        [PSCustomObject]@{
            Name = $_.NameExchange
            pref = $_.Preference
            ttl  = $_.ttl
        }
    }
}

$SPF = Resolve-DnsName -Name $TestDomain -Type 'TXT'-Server '8.8.8.8' -DnsOnly -ErrorAction SilentlyContinue
$resultspf = $false
foreach ($Item in $SPf.strings) {
    if ($Item -match "v=spf1") {
        [string]$resultspf = $Item
    }
}

$DMARC = Resolve-DnsName -Name "_dmarc.$($TestDomain)" -Type 'TXT' -Server '8.8.8.8' -DnsOnly -ErrorAction SilentlyContinue
$resultdmarc = $false
foreach ($Item in $DMARC) {
    if ($Item.type -eq 'txt') {
        [string]$resultdmarc = $Item.Strings
    }
}

#Output
Return [PSCustomObject]@{
    A        = $resultA
    MX       = $resultmx
    SPF      = $resultspf
    DMARC    = $resultdmarc
    DOMAIN   = $TestDomain
}

This is my script above. when using a domain like Google.com in my script.

the output is like this

A      : True
MX     : {@{Name=smtp.google.com; pref=10; ttl=300}}
SPF    : v=spf1 include:_spf.google.com ~all
DMARC  : v=DMARC1; p=reject; rua=mailto:mailauth-reports@google.com
DOMAIN : google.com

How do I expand that result in MX part of the object that is return. If I go $resultmx the return is how I expect like

Name pref ttl


smtp.google.com 10 300

the output of the variable is fine but when nested inside the return object it is not expanded.

dcaz
  • 847
  • 6
  • 15
  • Seems like you're looking to merge the output from `Type A` with `Type MX`, is that right? – Santiago Squarzon Oct 11 '22 at 21:10
  • Its that ```$resultmx``` once that variable has the content if I go $resultmx its fine but inside the output object it is {@{ and , – dcaz Oct 11 '22 at 21:13
  • I want the result to expand as if I typed $resultmx. – dcaz Oct 11 '22 at 21:13
  • how should the expected object look? that property is a object with 3 properties, `Name`, `pref` and `ttl`. Do you want a specific property? – Santiago Squarzon Oct 11 '22 at 21:14
  • all 3 properties. I guess my question was not clear enough. Sorry – dcaz Oct 11 '22 at 21:18
  • 1
    you can do something like `[PSCustomObject]@{ A = ...; 'MX.Name' = $resultmx.Name; 'MX.pref' = $resultmx.pref; 'MX.ttl' = $resultmx.ttl; SPF = $resultspf... }` not sure if thats what you want – Santiago Squarzon Oct 11 '22 at 21:22
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/248741/discussion-between-dcaz-and-santiago-squarzon). – dcaz Oct 11 '22 at 21:24
  • 1
    See: [Convert nested JSON array into separate columns in CSV file](https://stackoverflow.com/a/46081131/1701026) – iRon Oct 12 '22 at 07:09

1 Answers1

0

this did not work out well as a question. I thought it was easy. I just did the following.

<#
This is my current favorite creation. A simple script that tries to find if there are the following DNS records A,MX,SPF,DMARC and DKIM.

Run the script and enter the full domain name or an email address.

Examples:
.\isdomainactive.ps1 -domain facebook.com

switch -sub will test the subdomain
.\isdomainactive.ps1 -domain cnn.facebook.com -sub

switch -more will provide value of MX,SPF,DMARC and DKIM (if -Selector is used)
.\isdomainactive.ps1 -domain cnn.facebook.com -sub -more -selector face
.\isdomainactive.ps1 -domain cnn.facebook.com -sub -selector face
.\isdomainactive.ps1 -domain cnn.facebook.com -selector face
.\isdomainactive.ps1 -domain cnn.facebook.com -sub -more
.\isdomainactive.ps1 -domain cnn.facebook.com -more
.\isdomainactive.ps1 -domain cnn.facebook.com -sub -more -selector face
.\isdomainactive.ps1 -domain cnn.facebook.com -sub -selector face
.\isdomainactive.ps1 -domain cnn.facebook.com -selector face

Results if any comes back as an object and on host.
#>
param (    
    [parameter(Mandatory = $true,
        HelpMessage = "Enter the full domain name. Example Facebook.com or enter an entire email address.")]
    [ValidateScript({
            if ($_ -like "*.*") {
                return $true
            }
            else {
                Throw [System.Management.Automation.ValidationMetadataException] "Enter a domain name like facebook.com or an entire email address."
            }
        })][string]$Domain,
    [parameter(Mandatory = $false,
        HelpMessage = "Allow subdomain. Example mail.facebook.com")][switch]$Sub,
    [parameter(Mandatory = $false,
        HelpMessage = "Return A,MX,SPF,DMARC records detail if any. DKIM needs -Selector and string for DKIM detail to appear.")][switch]$More,
    [parameter(Mandatory = $false,
        HelpMessage = "DKIM selector. DKIM won't be checked without this string.")][string]$Selector
)

<#
ver 2, Author Dan Casmas 10/2022. Designed to work on Windows OS.
Has only been tested with 5.1 and 7 PS Versions. Requires a minimum of PS 5.1
Parts of this code was written by Jordan W.
#>
#Requires -Version 5.1

#if email address pull down to domain and if not test domain
$TestDomain = $null
Try {
    $TestDomain = ([Net.Mail.MailAddress]$Domain).Host
}
Catch {
    [string]$TestDomain = $Domain
}

#Removes @
If ($TestDomain -like '@*') {
    [string]$TestDomain = $TestDomain.Replace('@', '').Trim()
}

#get the last two items in the array and join them with dot
if (-not $Sub) {
    [string]$TestDomain = $TestDomain.Split(".")[-2, -1] -join "."
}

#checks if -Selector is not used
if ([string]::IsNullOrWhiteSpace($Selector)) {
    $Selector = 'unchecked'
    $resultdkim = 'unchecked'
}

[string]$resultA = If (Resolve-DnsName -Name $TestDomain -Type 'A' -Server '8.8.8.8' -DnsOnly -ErrorAction SilentlyContinue | Where-Object { $_.type -eq 'a' } ) { $true } Else { $false }

#more detail on the return
$resultmx = $null
If ($More) {
    #Brings back each record
    $mx = Resolve-DnsName -Name $TestDomain -Type 'MX' -Server '8.8.8.8' -DnsOnly -ErrorAction SilentlyContinue | Sort-Object -Property Preference -ErrorAction SilentlyContinue
    if ([string]::IsNullOrWhiteSpace($mx.NameExchange)) {
        [string]$resultmx = $false
    }
    Else {
        [string]$resultmx = "name pref ttl`n"  
        foreach ($Item in $mx) {
            [string]$resultmx += $Item.NameExchange + " $($Item.Preference)" + " $($Item.ttl)`n"
        }
    }

    $SPF = Resolve-DnsName -Name $TestDomain -Type 'TXT'-Server '8.8.8.8' -DnsOnly -ErrorAction SilentlyContinue
    $resultspf = $false
    foreach ($Item in $SPf.strings) {
        if ($Item -match "v=spf1") {
            [string]$resultspf = $Item
        }
    }

    $DMARC = Resolve-DnsName -Name "_dmarc.$($TestDomain)" -Type 'TXT' -Server '8.8.8.8' -DnsOnly -ErrorAction SilentlyContinue
    $resultdmarc = $false
    foreach ($Item in $DMARC) {
        if ($Item.type -eq 'txt') {
            [string]$resultdmarc = $Item.Strings
        }
    }
   
    if ($Selector -ne 'unchecked') {
        $DKIM = Resolve-DnsName -Type 'TXT' -Name "$($Selector)._domainkey.$($TestDomain)" -Server '8.8.8.8' -DnsOnly -ErrorAction SilentlyContinue
        $resultdkim = $false
        foreach ($Item in $DKIM) {
            if ($Item.type -eq 'txt') {
                [string]$resultdkim = $Item.Strings
            }
        }
    }
}
Else {
    #brings back True or False
    [string]$resultmx = If (Resolve-DnsName -Name $TestDomain -Type 'MX' -Server '8.8.8.8' -DnsOnly -ErrorAction SilentlyContinue | Where-Object { $_.type -eq 'mx' } ) { $true } Else { $false }

    if ($Selector -ne 'unchecked') {
        [string]$resultdkim = If (Resolve-DnsName -Type 'TXT' -Name "$($Selector)._domainkey.$($TestDomain)" -Server '8.8.8.8' -DnsOnly -ErrorAction SilentlyContinue | where-object { $_.strings -match "v=DKIM1" } ) { $true } Else { $false }
    }

    [string]$resultspf = If (Resolve-DnsName -Name $TestDomain -Type 'TXT'-Server '8.8.8.8' -DnsOnly -ErrorAction SilentlyContinue | where-object { $_.strings -match "v=spf1" } ) { $true } Else { $false }

    [string]$resultDMARC = if (Resolve-DnsName -Name "_dmarc.$($TestDomain)" -Type 'TXT' -Server '8.8.8.8' -DnsOnly -ErrorAction SilentlyContinue | Where-Object { $_.type -eq 'txt' } ) { $true } Else { $false }
}

#Output
Return [PSCustomObject]@{
    A        = $resultA
    MX       = $resultmx.trimend("`r`n")
    SPF      = $resultspf
    DMARC    = $resultdmarc
    DKIM     = $resultdkim
    Selector = $Selector
    DOMAIN   = $TestDomain
}
dcaz
  • 847
  • 6
  • 15