2021-10-30

Get DNS script has missing output in report

<# 
Gets the various dns record type for a domain or use -RecordType all for all and -Report to file output.
Use like .\get-dnsrecords.ps1 -Name Facebook -RecordType all or .\get-dnsrecords.ps1 -name facebook -RecordType MX
#>
param(
    [Parameter(Mandatory = $True,
        ValueFromPipelineByPropertyName = $True,
        HelpMessage = "Specifies the domain.")]
    [string]$Name,

    [Parameter(Mandatory = $False,
        HelpMessage = "Which Record.")]
    [ValidateSet('A', 'MX', 'TXT', 'All')]
    [string]$RecordType = 'txt',

    [Parameter(Mandatory = $false,
        HelpMessage = "DNS Server to use.")]
    [string]$Server = '8.8.8.8',
  
    [Parameter(Mandatory = $false,
        HelpMessage = "Make a csv report in c:\Reports")]
    [Switch]$Report
)

IF ($Name -notlike "*.*") {
    $Name = $Name + '.com'
}

If ($Report) {
    $filename = [environment]::getfolderpath("mydocuments") + '\' + $($RecordType) + '-' + ($Name.Split('.')[0]) + '.csv'
}

If ($RecordType -eq 'txt' -or $RecordType -eq 'All') {
    $TXTRecord = Resolve-DnsName $Name -Type txt -Server $Server -ErrorAction Stop | ForEach-Object {
        [PSCustomObject][ordered]@{
            name    = $_.name
            type    = $_.Type
            ttl     = $_.ttl
            section = $_.Section
            strings = ($_.strings | Out-String).trim()
        }
    }

    If ($RecordType -eq 'txt') {
        $TXTRecord
        If ($Report) {
            $TXTRecord | Export-Csv $filename -NoTypeInformation -Delimiter ','
        }
        $TXTRecord
        Return write-host $filename -ForegroundColor blue
    }
}

If ($RecordType -eq 'mx' -or $RecordType -eq 'All' ) {
    $MXRecord = Resolve-DnsName $Name -Type mx -Server $Server -ErrorAction Stop | ForEach-Object {
        [PSCustomObject]@{
            Name         = $_.name
            Type         = $_.type
            TTL          = $_.ttl
            Section      = $_.section
            NameExchange = $_.nameexchange
        }
    }

    If ($RecordType -eq 'MX') {
        If ($Report) {
            $MXRecord | Export-Csv $filename -NoTypeInformation
        }
        $MXRecord
        Return  Write-Host $filename -ForegroundColor blue
    }
}

If ($RecordType -eq 'a' -or $RecordType -eq 'All' ) {
    $ARecord = Resolve-DnsName $Name -Type A -Server $Server -ErrorAction Stop | ForEach-Object {
        [PSCustomObject]@{
            Name       = $_.name
            Type       = $_.type
            TTL        = $_.ttl
            Section    = $_.section
            IP4Address = $_.IP4Address
        }
    }

    If ($RecordType -eq 'a') {
        If ($Report) {
            $ARecord | Export-Csv $filename -NoTypeInformation
        }
        $ARecord
        Return write-host $filename -ForegroundColor blue
    }
}

If ($Report) {
    $TXTRecord | Export-Csv $filename -NoTypeInformation
    $MXRecord | Select-Object name, Type, ttyl, section, NameExchange | Export-Csv $filename -NoTypeInformation -Append -Force
    $ARecord | Select-Object name, Type, ttyl, section, IP4Address | Export-Csv $filename -NoTypeInformation -Append -Force
}
$TXTRecord
$MXRecord
$ARecord 
Return Write-Host $filename -ForegroundColor blue

Running .\get-dnsrecords.ps1 -Name Facebook -RecordType all -Report

Example report Output file is like the following and the host blast is ugly as well.

"name","type","ttl","section","strings"
"Facebook.com","TXT","3600","Answer","String"
"Facebook.com","TXT","3600","Answer","String"
"FaceBook.com","MX","21001","Answer",
"FaceBook.com","MX","21001","Answer",
"FaceBook.com","A","20","Answer",

Mx is missing NameExchange

A record is missing IP4Address

Ideas on how to make the report output include the missing items and bonus how to make the host output more readable ?

The problem is when I try to combine the output variables at the end and then export to file. I am just not sure how to correct it.



from Recent Questions - Stack Overflow https://ift.tt/3mraM3J
https://ift.tt/eA8V8J

No comments:

Post a Comment