From 1cdc28086cad4c6606bdcf837c80708fe7959f5c Mon Sep 17 00:00:00 2001 From: Przemyslaw Klys Date: Mon, 3 Aug 2020 23:32:21 +0200 Subject: [PATCH] Updated Find- cmdlets --- Public/Find-DKIMRecord.ps1 | 66 +++++++++++++++++++++---------------- Public/Find-DMARCRecord.ps1 | 24 +++++++++----- Public/Find-MXRecord.ps1 | 30 ++++++++++------- Public/Find-SPFRecord.ps1 | 24 +++++++++----- 4 files changed, 88 insertions(+), 56 deletions(-) diff --git a/Public/Find-DKIMRecord.ps1 b/Public/Find-DKIMRecord.ps1 index 32ca8dd..bebe686 100644 --- a/Public/Find-DKIMRecord.ps1 +++ b/Public/Find-DKIMRecord.ps1 @@ -1,45 +1,53 @@ function Find-DKIMRecord { [CmdletBinding()] param ( - [Parameter(Mandatory, ValueFromPipelineByPropertyName, ValueFromPipeline, Position = 0)][string[]] $DomainName, - [string[]] $Selector = "selector1", + [Parameter(Mandatory, ValueFromPipelineByPropertyName, ValueFromPipeline, Position = 0)][Array] $DomainName, + [string] $Selector = 'selector1', [System.Net.IPAddress] $DnsServer, [switch] $AsHashTable, [switch] $AsObject ) process { foreach ($Domain in $DomainName) { - foreach ($S in $Selector) { - $Splat = @{ - Name = "$S._domainkey.$Domain" - Type = "TXT" - ErrorAction = "SilentlyContinue" + if ($Domain -is [string]) { + $S = $Selector + $D = $Domain + } elseif ($Domain -is [System.Collections.IDictionary]) { + $S = $Domain.Selector + $D = $Domain.DomainName + if (-not $S -and -not $D) { + Write-Warning 'Find-DKIMRecord - properties DomainName and Selector are required when passing Array of Hashtables' } - if ($DnsServer) { - $Splat['Server'] = $DnsServer - } - $DNSRecord = Resolve-DnsQuery @Splat | Where-Object Text -Match "DKIM1" - if (-not $AsObject) { - $MailRecord = [ordered] @{ - Name = $Domain - Count = $DNSRecord.Text.Count - Selector = "$Domain`:$S" - DKIM = $DNSRecord.Text -join '; ' - } - } else { - $MailRecord = [ordered] @{ - Name = $Domain - Count = $DNSRecord.Text.Count - Selector = "$Domain`:$S" - DKIM = $DNSRecord.Text -join '; ' - } + } + $Splat = @{ + Name = "$S._domainkey.$D" + Type = 'TXT' + ErrorAction = 'SilentlyContinue' + } + if ($DnsServer) { + $Splat['Server'] = $DnsServer + } + $DNSRecord = Resolve-DnsQuery @Splat | Where-Object Text -Match 'DKIM1' + if (-not $AsObject) { + $MailRecord = [ordered] @{ + Name = $D + Count = $DNSRecord.Text.Count + Selector = "$D`:$S" + DKIM = $DNSRecord.Text -join '; ' } - if ($AsHashTable) { - $MailRecord - } else { - [PSCustomObject] $MailRecord + } else { + $MailRecord = [ordered] @{ + Name = $D + Count = $DNSRecord.Text.Count + Selector = "$D`:$S" + DKIM = $DNSRecord.Text -join '; ' } } + if ($AsHashTable) { + $MailRecord + } else { + [PSCustomObject] $MailRecord + } } } } \ No newline at end of file diff --git a/Public/Find-DMARCRecord.ps1 b/Public/Find-DMARCRecord.ps1 index 04d70aa..f38d5fd 100644 --- a/Public/Find-DMARCRecord.ps1 +++ b/Public/Find-DMARCRecord.ps1 @@ -1,33 +1,41 @@ function Find-DMARCRecord { [CmdletBinding()] param ( - [Parameter(Mandatory, ValueFromPipelineByPropertyName, ValueFromPipeline, Position = 0)][string[]] $DomainName, + [Parameter(Mandatory, ValueFromPipelineByPropertyName, ValueFromPipeline, Position = 0)][Array] $DomainName, [System.Net.IPAddress] $DnsServer, [switch] $AsHashTable, [switch] $AsObject ) process { foreach ($Domain in $DomainName) { + if ($Domain -is [string]) { + $D = $Domain + } elseif ($Domain -is [System.Collections.IDictionary]) { + $D = $Domain.DomainName + if (-not $D) { + Write-Warning 'Find-DMARCRecord - property DomainName is required when passing Array of Hashtables' + } + } $Splat = @{ - Name = "_dmarc.$Domain" - Type = "TXT" - ErrorAction = "Stop" + Name = "_dmarc.$D" + Type = 'TXT' + ErrorAction = 'Stop' } if ($DnsServer) { $Splat['Server'] = $DnsServer } try { - $DNSRecord = Resolve-DnsQuery @Splat | Where-Object Text -Match "DMARC1" + $DNSRecord = Resolve-DnsQuery @Splat | Where-Object Text -Match 'DMARC1' if (-not $AsObject) { $MailRecord = [ordered] @{ - Name = $Domain + Name = $D Count = $DNSRecord.Count TimeToLive = $DnsRecord.TimeToLive -join '; ' DMARC = $DnsRecord.Text -join '; ' } } else { $MailRecord = [ordered] @{ - Name = $Domain + Name = $D Count = $DNSRecord.Count TimeToLive = $DnsRecord.TimeToLive DMARC = $DnsRecord.Text @@ -35,7 +43,7 @@ function Find-DMARCRecord { } } catch { $MailRecord = [ordered] @{ - Name = $Domain + Name = $D Count = 0 TimeToLive = '' DMARC = '' diff --git a/Public/Find-MXRecord.ps1 b/Public/Find-MXRecord.ps1 index c0d09d5..cde9286 100644 --- a/Public/Find-MXRecord.ps1 +++ b/Public/Find-MXRecord.ps1 @@ -1,7 +1,7 @@ function Find-MxRecord { [CmdletBinding()] param ( - [Parameter(Mandatory, ValueFromPipelineByPropertyName, ValueFromPipeline, Position = 0)][string[]]$DomainName, + [Parameter(Mandatory, ValueFromPipelineByPropertyName, ValueFromPipeline, Position = 0)][Array]$DomainName, [System.Net.IPAddress] $DnsServer, [switch] $ResolvePTR, [switch] $AsHashTable, @@ -10,10 +10,18 @@ function Find-MxRecord { ) process { foreach ($Domain in $DomainName) { + if ($Domain -is [string]) { + $D = $Domain + } elseif ($Domain -is [System.Collections.IDictionary]) { + $D = $Domain.DomainName + if (-not $D) { + Write-Warning 'Find-MxRecord - property DomainName is required when passing Array of Hashtables' + } + } $Splat = @{ - Name = $Domain - Type = "MX" - ErrorAction = "SilentlyContinue" + Name = $D + Type = 'MX' + ErrorAction = 'SilentlyContinue' } if ($DnsServer) { $Splat['Server'] = $DnsServer @@ -21,16 +29,16 @@ function Find-MxRecord { $MX = Resolve-DnsQuery @Splat [Array] $MXRecords = foreach ($MXRecord in $MX) { $MailRecord = [ordered] @{ - Name = $Domain + Name = $D Preference = $MXRecord.Preference TimeToLive = $MXRecord.TimeToLive - MX = ($MXRecord.Exchange) -replace ".$" + MX = ($MXRecord.Exchange) -replace '.$' } [Array] $IPAddresses = foreach ($Record in $MX.Exchange) { $Splat = @{ Name = $Record Type = 'A' - ErrorAction = "SilentlyContinue" + ErrorAction = 'SilentlyContinue' } if ($DnsServer) { $Splat['Server'] = $DnsServer @@ -43,12 +51,12 @@ function Find-MxRecord { $Splat = @{ Name = $IP Type = 'PTR' - ErrorAction = "SilentlyContinue" + ErrorAction = 'SilentlyContinue' } if ($DnsServer) { $Splat['Server'] = $DnsServer } - (Resolve-DnsQuery @Splat) | ForEach-Object { $_.PtrDomainName -replace ".$" } + (Resolve-DnsQuery @Splat) | ForEach-Object { $_.PtrDomainName -replace '.$' } } } $MailRecord @@ -64,7 +72,7 @@ function Find-MxRecord { } else { if (-not $AsObject) { $MXRecord = [ordered] @{ - Name = $Domain + Name = $D Count = $MXRecords.Count Preference = $MXRecords.Preference -join '; ' TimeToLive = $MXRecords.TimeToLive -join '; ' @@ -76,7 +84,7 @@ function Find-MxRecord { } } else { $MXRecord = [ordered] @{ - Name = $Domain + Name = $D Count = $MXRecords.Count Preference = $MXRecords.Preference TimeToLive = $MXRecords.TimeToLive diff --git a/Public/Find-SPFRecord.ps1 b/Public/Find-SPFRecord.ps1 index 422c9db..87184e6 100644 --- a/Public/Find-SPFRecord.ps1 +++ b/Public/Find-SPFRecord.ps1 @@ -1,33 +1,41 @@ function Find-SPFRecord { [CmdletBinding()] param ( - [Parameter(Mandatory, ValueFromPipelineByPropertyName, ValueFromPipeline, Position = 0)][string[]]$DomainName, + [Parameter(Mandatory, ValueFromPipelineByPropertyName, ValueFromPipeline, Position = 0)][Array]$DomainName, [System.Net.IPAddress] $DnsServer, [switch] $AsHashTable, [switch] $AsObject ) process { foreach ($Domain in $DomainName) { + if ($Domain -is [string]) { + $D = $Domain + } elseif ($Domain -is [System.Collections.IDictionary]) { + $D = $Domain.DomainName + if (-not $D) { + Write-Warning 'Find-MxRecord - property DomainName is required when passing Array of Hashtables' + } + } $Splat = @{ - Name = $Domain - Type = "txt" - ErrorAction = "Stop" + Name = $D + Type = 'txt' + ErrorAction = 'Stop' } if ($DnsServer) { $Splat['Server'] = $DnsServer } try { - $DNSRecord = Resolve-DnsQuery @Splat | Where-Object Text -Match "spf1" + $DNSRecord = Resolve-DnsQuery @Splat | Where-Object Text -Match 'spf1' if (-not $AsObject) { $MailRecord = [ordered] @{ - Name = $Domain + Name = $D Count = $DNSRecord.Count TimeToLive = $DnsRecord.TimeToLive -join '; ' SPF = $DnsRecord.Text -join '; ' } } else { $MailRecord = [ordered] @{ - Name = $Domain + Name = $D Count = $DNSRecord.Count TimeToLive = $DnsRecord.TimeToLive SPF = $DnsRecord.Text @@ -35,7 +43,7 @@ function Find-SPFRecord { } } catch { $MailRecord = [ordered] @{ - Name = $Domain + Name = $D Count = 0 TimeToLive = '' SPF = ''