CSV Exporting DNS information - Need help

Welcome Forums General PowerShell Q&A CSV Exporting DNS information - Need help

This topic contains 6 replies, has 2 voices, and was last updated by

 
Participant
1 month, 3 weeks ago.

  • Author
    Posts
  • #167692

    Participant
    Topics: 1
    Replies: 3
    Points: 18
    Rank: Member

    Hi there people!

    I am struggling with trying to reshape an existing command.
    Credit for this original command goes to https://www.xpertnotes.net/blog/2017/03/27/bulk-domain-look-ups-using-power-shell/.

    However, what i need to do, is instead of having a TXT file spat out, i need a CSV file showing the domains listed in this order:
    Domainname, Nameserver, A-record, Mx-Record.

    I have tried the following but the csv file is empty.

    $ErrorActionPreference = 'SilentlyContinue'
    
    # This is where we are going to put the results
    $logfile = "C:\Users\jonat\Desktop\DNS PROCCESSING\Output\result.txt"
    
    # If there is an old log, delete, we don't need it.
    Remove-Item $logfile
    
    #Pick a name server to query
    $nameserver = "DNSSERVER"
    
    $domains = get-content "C:\Users\jonat\Desktop\DNS PROCCESSING\INPUT-ST15.txt"
    
    # Get Busy
    foreach ($domain in $domains){
    
    $domain | Out-file -filepath $logfile -Append -width 180
    
    # NSLOOKUP COMMANDS run one or all - examples are given
    
    #nslookup -querytype=ANY $domain $nameserver | Out-file -filepath $logfile -Append -width 180
    nslookup -querytype=A $domain $nameserver | Out-file -filepath $logfile -Append -width 180
    #nslookup -querytype=CNAME $domain $nameserver | Out-file -filepath $logfile -Append -width 180
    nslookup -querytype=NS $domain $nameserver | Out-file -filepath $logfile -Append -width 180
    nslookup -querytype=MX $domain $nameserver | Out-file -filepath $logfile -Append -width 180
    #nslookup -querytype=SRV $domain $nameserver | Out-file -filepath $logfile -Append -width 180
    
    "-----------------------------------------------------------------" | Out-file -filepath $logfile -Append -width 180
    " " | Out-file -filepath $logfile -Append -width 180
    }
    #futile attempt on csv exporting
    Get-Process | Select-Object -Property internet name,address,mail exchanger,address |
    Export-Csv -Path "C:\Users\jonat\Desktop\DNS PROCCESSING\Output\result-csv.csv" -NoTypeInformation
    
    Write-host "Job Complete please view $logfile"

    Right now, the command just spits out the TXT, which does contain all that i need though.
    And example looks like this:

    magasin.dk
    Server: UnKnown
    Address: 91.224.174.138
    
    Name: magasin.dk
    Address: 193.88.99.152
    
    Server: UnKnown
    Address: 91.224.174.138
    
    magasin.dk nameserver = ns2.tdchweb.dk
    magasin.dk nameserver = ns1.tdchweb.dk
    
    ns2.tdchweb.dk internet address = 193.88.99.143
    ns1.tdchweb.dk internet address = 193.88.99.205
    Server: UnKnown
    Address: 91.224.174.138
    
    magasin.dk MX preference = 20, mail exchanger = mailgw.magasin.dk
    
    mailgw.magasin.dk internet address = 194.255.89.228
    -----------------------------------------------------------------

    I really hope a kind soul can help me figure this out. According to the many forums i have looked at this should be simple.
    Not for me however, this is my first day trying out Powershell.

    The whole "Get-Process" and "Select-Object" thing, is not something i understand yet..

    Hopeful regards
    Jonatan

  • #167704

    Participant
    Topics: 9
    Replies: 423
    Points: 675
    Helping Hand
    Rank: Major Contributor

    "i need a CSV file showing the domains listed in this order:
    Domainname, Nameserver, A-record, Mx-Record."

    You realize that most domains have multiple name servers, a-records, and mx-records, right?

  • #167707

    Participant
    Topics: 9
    Replies: 423
    Points: 675
    Helping Hand
    Rank: Major Contributor
    #region Input
    
    $DomainList = @(
        'cnn.com'
        'google.com'
        'powershell.org'
        'bla.bla'
    )
    
    # or 
    
    $DomainList = Get-Content .\domainlist.txt
    
    # or
    
    $DomainList = (Import-Csv .\domainlist.csv).domainname # where there's a column called 'domainname' 
    
    #endregion
    
    
    #region Process
    
    $myOutput = foreach ($DomainName in $DomainList) {
        try {
            $NSRecords = Resolve-DnsName $DomainName -Type NS -EA 1 | where { $_.QueryType -eq 'NS' }
            [PSCustomObject]@{
                DomainName  = $DomainName
                Resolved    = $true
                NameServer  = $NSRecords.NameHost
                'A-Record'  = (Resolve-DnsName $DomainName -Type A).IPAddress
                'MX-Record' = (Resolve-DnsName $DomainName -Type MX).NameExchange
            }
        } catch {
            [PSCustomObject]@{
                DomainName  = $DomainName
                Resolved    = $false
                NameServer  = '?'
                'A-Record'  = '?'
                'MX-Record' = '?'
            }
        }
    }
    
    #endregion
    
    
    #region Output
    
    $myOutput | FL 
    
    # or
    $myOutput | select DomainName, Resolved,
        @{n='NameServer';e={$_.NameServer -join ', '}},
        @{n='A-Record';e={$_.'A-Record' -join ', '}},
        @{n='MX-Record';e={$_.'MX-Record' -join ', '}} | 
            Export-Csv .\DomainReport.csv -NoType 
    
    # or 
    $myOutput | Out-GridView
    
    #endregion
    
  • #167713

    Participant
    Topics: 1
    Replies: 3
    Points: 18
    Rank: Member

    Hi Sam!

    You have no idea how much you just saved my day!!
    It works perfectly.

    – i know there are often several records, these are fine to have included too!
    I have to filter 2500 domains and figure out what we have and dont have active and such. This will make it humanly possible.

    Thank you so much, and have an awesome day! 🙂

    Best regards
    Jonatan

  • #167746

    Participant
    Topics: 1
    Replies: 3
    Points: 18
    Rank: Member

    Hi again!

    If its not too much trouble i have a small followup question 🙂

    I want to call several different .txt files.

    For an example, serverlist-1.txt, serverlist-2.txt and so on.

    I have changed this to work by changing to the following:

    $DomainList = Get-Content "C:\Users\jonat\Desktop\DNS PROCCESSING\Batchtest\*.txt" 
    $contentorigin = (Get-ChildItem "C:\Users\jonat\Desktop\DNS PROCCESSING\Batchtest\*.txt").fullname
    
    #region Process
    
    $myOutput = foreach ($DomainName in $DomainList) {
        try {
            $NSRecords = Resolve-DnsName $DomainName -Type NS -EA 1 | where { $_.QueryType -eq 'NS' }
            [PSCustomObject]@{
                DomainName  = $DomainName
                Resolved    = $true
                NameServer  = $NSRecords.NameHost
                'A-Record'  = (Resolve-DnsName $DomainName -Type A).IPAddress
                'MX-Record' = (Resolve-DnsName $DomainName -Type MX).NameExchange
                Origin = $contentorigin
                
            }
        } catch {
            [PSCustomObject]@{
                DomainName  = $DomainName
                Resolved    = $false
                NameServer  = '?'
                'A-Record'  = '?'
                'MX-Record' = '?'
                Origin = $contentorigin
            }
        }
    }
    
    #endregion
    
    
    #region Output
    
    $myOutput | FL 
    
    # or
    $myOutput | select DomainName, Resolved,
        @{n='NameServer';e={$_.NameServer -join ', '}},
        @{n='A-Record';e={$_.'A-Record' -join ', '}},
        @{n='MX-Record';e={$_.'MX-Record' -join ', '}},
        @{n='Origin';e={$_.Origin}} | 
        
            Export-Csv "C:\Users\jonat\Desktop\DNS PROCCESSING\Output\result-export.csv" -NoType 
    
    # or 
    $myOutput | Out-GridView
    
    #endregion
    
    
    

    Which is actually working very well, with the little problem that the new field "Origin" shows both file names for every domain, even though they came from different documents.

    A Domain name coming from st1.txt, shows borht st1.txt and st2.txt as shown here:

    "domainnametest.dk","True","ns2.surf-town.net, ns3.surf-town.net, ns1.surf-town.net","212.97.132.203","mailb12.surf-town.net","C:\Users\jonat\Desktop\DNS PROCCESSING\Batchtest\ST1.txt C:\Users\jonat\Desktop\DNS PROCCESSING\Batchtest\ST2.txt"
    
    "domainnametest2.dk","True","ns2.surf-town.net, ns3.surf-town.net, ns1.surf-town.net","212.97.132.203","mailb12.surf-town.net","C:\Users\jonat\Desktop\DNS PROCCESSING\Batchtest\ST1.txt C:\Users\jonat\Desktop\DNS PROCCESSING\Batchtest\ST2.txt"
    

    What i am trying to accomplish is for the CSV to inform me, which .txt document the domain in question originated from.
    So what i would love to see from the export looking like this instead:

    "domainnametest.dk","True","ns2.surf-town.net, ns3.surf-town.net, ns1.surf-town.net","212.97.132.203","mailb12.surf-town.net","C:\Users\jonat\Desktop\DNS PROCCESSING\Batchtest\ST1.txt"
    
    "domainnametest2.dk","True","ns2.surf-town.net, ns3.surf-town.net, ns1.surf-town.net","212.97.132.203","mailb12.surf-town.net","C:\Users\jonat\Desktop\DNS PROCCESSING\Batchtest\ST2.txt"
    

    Is there an easy way of correcting this? 🙂

  • #167755

    Participant
    Topics: 9
    Replies: 423
    Points: 675
    Helping Hand
    Rank: Major Contributor
    $FileList = (Get-ChildItem 'C:\Users\jonat\Desktop\DNS PROCCESSING\Batchtest\*.txt').fullname
    
    $myOutput = @() # Declare empty array
    foreach ($FileName in $FileList) {
        $myOutput += foreach ($DomainName in (Get-Content $FileName)) {
            try {
                $NSRecords = Resolve-DnsName $DomainName -Type NS -EA 1 | where { $_.QueryType -eq 'NS' }
                [PSCustomObject]@{
                    Origin      = $FileName
                    DomainName  = $DomainName
                    Resolved    = $true
                    NameServer  = $NSRecords.NameHost
                    'A-Record'  = (Resolve-DnsName $DomainName -Type A).IPAddress
                    'MX-Record' = (Resolve-DnsName $DomainName -Type MX).NameExchange            
                }
            } catch {
                [PSCustomObject]@{
                    Origin      = $FileName
                    DomainName  = $DomainName
                    Resolved    = $false
                    NameServer  = '?'
                    'A-Record'  = '?'
                    'MX-Record' = '?'
                }
            }
        }
    }
    
    $myOutput | select Origin, DomainName, Resolved,
        @{n='NameServer';e={$_.NameServer  -join ', '}},
        @{n='A-Record';  e={$_.'A-Record'  -join ', '}},
        @{n='MX-Record'; e={$_.'MX-Record' -join ', '}} |     
            Export-Csv 'C:\Users\jonat\Desktop\DNS PROCCESSING\Output\result-export.csv' -NoType 
    
    

    I'm being lazy by just spitting out the code. I should point out what you did wrong and why, so you can learn, which is the purpose of this forum. Alternatively, I ask that you go through the code and see if you understand the purpose/reason for each line and each choice made. Try to search for answers. If not ask here.

  • #167893

    Participant
    Topics: 1
    Replies: 3
    Points: 18
    Rank: Member

    Hi again Sam!

    I will be digging in into the code to understand it as best i can.
    I am a Rookie in PowerShell, so i have some way to go 🙂

    Thanks again for you quick response and help!

    Best regards
    Jonatan

You must be logged in to reply to this topic.