Skip to content

Commit

Permalink
Updated Find- cmdlets
Browse files Browse the repository at this point in the history
  • Loading branch information
PrzemyslawKlys committed Aug 3, 2020
1 parent ea60bc2 commit 1cdc280
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 56 deletions.
66 changes: 37 additions & 29 deletions Public/Find-DKIMRecord.ps1
Original file line number Diff line number Diff line change
@@ -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
}
}
}
}
24 changes: 16 additions & 8 deletions Public/Find-DMARCRecord.ps1
Original file line number Diff line number Diff line change
@@ -1,41 +1,49 @@
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
}
}
} catch {
$MailRecord = [ordered] @{
Name = $Domain
Name = $D
Count = 0
TimeToLive = ''
DMARC = ''
Expand Down
30 changes: 19 additions & 11 deletions Public/Find-MXRecord.ps1
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -10,27 +10,35 @@ 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
}
$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
Expand All @@ -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
Expand All @@ -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 '; '
Expand All @@ -76,7 +84,7 @@ function Find-MxRecord {
}
} else {
$MXRecord = [ordered] @{
Name = $Domain
Name = $D
Count = $MXRecords.Count
Preference = $MXRecords.Preference
TimeToLive = $MXRecords.TimeToLive
Expand Down
24 changes: 16 additions & 8 deletions Public/Find-SPFRecord.ps1
Original file line number Diff line number Diff line change
@@ -1,41 +1,49 @@
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
}
}
} catch {
$MailRecord = [ordered] @{
Name = $Domain
Name = $D
Count = 0
TimeToLive = ''
SPF = ''
Expand Down

0 comments on commit 1cdc280

Please sign in to comment.