DomainHealthChecker.psm1

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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<#>
HelpInfoURI 'https://github.com/T13nn3s/Show-SpfDkimDmarc/blob/main/public/CmdletHelp/Invoke-SpfDkimDmarc.md'
#>


# Load functions
Get-ChildItem -Path $PSScriptRoot\public\*.ps1 | 
ForEach-Object {
    . $_.FullName
}

function Invoke-SpfDkimDmarc {
    [CmdletBinding()]
    param (
        [Parameter(
            Mandatory, ParameterSetName = 'domain',
            ValueFromPipeline = $True,
            ValueFromPipelineByPropertyName = $True,
            HelpMessage = "Specifies the domain for resolving the SPF, DKIM and DMARC-record.",
            Position = 1)]
        [string]$Name,

        [Parameter(
            Mandatory, ParameterSetName = 'file',
            ValueFromPipeline = $True,
            ValueFromPipelineByPropertyName = $True,
            HelpMessage = "Show SPF, DKIM and DMARC-records from multiple domains from a file.",
            Position = 2)]
        [Alias('Path')]
        [System.IO.FileInfo]$File,

        [Parameter(Mandatory = $False,
            HelpMessage = "Specify a custom DKIM selector.",
            Position = 3)]
        [string]$DkimSelector,

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

    begin {
        $InvokeObject = New-Object System.Collections.Generic.List[System.Object]        
    } process {
        function StartDomainHealthCheck($Name, $DkimSelector) {
            if ($DkimSelector) {
                $Splat = @{
                    'DkimSelector' = $DkimSelector
                }
            }

            $SPF = Get-SPFRecord -Name $Name
            $DKIM = Get-DKIMRecord -Name $Name @Splat
            $DMARC = Get-DMARCRecord -Name $Name

            $InvokeReturnValues = New-Object psobject
            $InvokeReturnValues | Add-Member NoteProperty "Name" $SPF.Name
            $InvokeReturnValues | Add-Member NoteProperty "SpfRecord" $SPF.SPFRecord
            $InvokeReturnValues | Add-Member NoteProperty "SpfAdvisory" $SPF.SpfAdvisory
            $InvokeReturnValues | Add-Member NoteProperty "DmarcRecord" $DMARC.DmarcRecord
            $InvokeReturnValues | Add-Member NoteProperty "DmarcAdvisory" $DMARC.DmarcAdvisory
            $InvokeReturnValues | Add-Member NoteProperty "DkimRecord" "$($DKIM.DkimRecord)"
            $InvokeReturnValues | Add-Member NoteProperty "DkimSelector" $DKIM.DkimSelector
            $InvokeReturnValues | Add-Member NoteProperty "DkimAdvisory" $DKIM.DkimAdvisory
            $InvokeObject.Add($InvokeReturnValues)
            $InvokeReturnValues
        }
    }
    end {
        if ($file) {
            foreach ($Name in (Get-Content -Path $File)) {
                StartDomainHealthCheck -Name $Name
            }
        }
        if ($Name) {
            StartDomainHealthCheck -Name $Name
        }
    }
}

Set-Alias Show-SpfDkimDmarc -Value Invoke-SpfDkimDmarc
Set-Alias isdd -Value Invoke-SpfDkimDmarc