public/Get-DMARCRecord.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
<#>
HelpInfoURI 'https://github.com/T13nn3s/Show-SpfDkimDmarc/blob/main/public/CmdletHelp/Get-DMARCRecord.md'
#>

function Get-DMARCRecord {
    [CmdletBinding()]
    param(
        [Parameter(
            Mandatory = $True,
            ValueFromPipeline = $True,
            ValueFromPipelineByPropertyName = $True,
            HelpMessage = "Specifies the domain for resolving the DMARC-record."
        )][string]$Name,

        [Parameter(Mandatory = $false,
            HelpMessage = "DNS Server to use.")]
        [string]$Server
    )

    begin {

        if ($PSBoundParameters.ContainsKey('Server')) {
            $SplatParameters = @{
                'Type'        = 'TXT'
                'Name'        = "_dmarc.$($Name)"
                'Server'      = $Server
                'ErrorAction' = 'SilentlyContinue'
            }
        }
        Else {
            $SplatParameters = @{
                'Type'        = 'TXT'
                'Name'        = "_dmarc.$($Name)"
                'ErrorAction' = 'SilentlyContinue'
            }
        }

        $DMARCObject = New-Object System.Collections.Generic.List[System.Object]
    }

    Process {
        $DMARC = Resolve-DnsName @SplatParameters | Select-Object -ExpandProperty strings -ErrorAction SilentlyContinue
        if ($DMARC -eq $null) {
            $DmarcAdvisory = "Does not have a DMARC record. This domain is at risk to being abused by phishers and spammers."
        }
        Else {
            switch -Regex ($DMARC) {
                ('p=none') {
                    $DmarcAdvisory = "Domain has a valid DMARC record but the DMARC (subdomain) policy does not prevent abuse of your domain by phishers and spammers."
                }
                ('p=quarantine') {
                    $DmarcAdvisory = "Domain has a DMARC record and it is set to p=quarantine. To fully take advantage of DMARC, the policy should be set to p=reject."
                }
                ('p=reject') {
                    $DmarcAdvisory = "Domain has a DMARC record and your DMARC policy will prevent abuse of your domain by phishers and spammers."
                }
            }
        }
    } 
    end {
        $DMARCReturnValues = New-Object psobject
        $DMARCReturnValues | Add-Member NoteProperty "Name" $Name
        $DMARCReturnValues | Add-Member NoteProperty "DmarcRecord" "$($DMARC)"
        $DMARCReturnValues | Add-Member NoteProperty "DmarcAdvisory" $DmarcAdvisory
        $DMARCObject.Add($DMARCReturnValues)
        $DMARCReturnValues
    }
}
Set-Alias -Name gdmarc -Value Get-DMARCRecord