A PowerShell script to take a list of domains and output their A and AAAA records as CSV

This was the result of a colloboration between me (with programming skills, but not in PowerShell!) and ChatGPT. It blows my mind away, what can be done with ChatGPT – this is what work IS supposed to be! Quick, without researching every single line. Human and machine in perfect colloboration:

# PowerShell script to get A and AAAA records for a list of domains, with debug output

# Define the path to the domain list and output CSV file
$domainListPath = "C:\your\path\domains.txt"
$outputCsvPath = ".\domain_records.csv"

# Read the list of domains from the file
$domains = Get-Content $domainListPath

# Prepare an array to hold the results
$results = @()

# Loop through each domain and query its A and AAAA records
foreach ($domain in $domains) {
    Write-Host "Processing domain: $domain"

    # Initialize the record object
    $record = New-Object PSObject -Property @{
        Domain = $domain
        ARecord = $null
        AAAARecord = $null
		WARecord = $null
		WAAAARecord = $null
    }

    Write-Host "  Performing nslookup..."
    # Perform nslookup for A and AAAA records
    $nslookupResults = nslookup $domain
	$nslookupResults = $nslookupResults | Select-Object -Skip 2

    # Extract A and AAAA records
    foreach ($line in $nslookupResults) {
		Write-Host "Debug output"
		Write-Host $line
        if ($line -match "(\d+\.\d+\.\d+\.\d+)") {
			$address = $matches[1]
			Write-Host "  Found A record: $address"
			$record.ARecord = $address
		} elseif ($line -match "([a-fA-F0-9:]+:[a-fA-F0-9:]+)") {
			$address = $matches[1]
			Write-Host "  Found AAAA record: $address"
			$record.AAAARecord = $address
		}
		
<#         if ($line -match "Address: (.*)") {
            $address = $matches[1]
            # Determine if the address is IPv4 (A record) or IPv6 (AAAA record)
            if ($address -match "\d+\.\d+\.\d+\.\d+") {
                Write-Host "  Found A record: $address"
                $record.ARecord = $address
            } elseif ($address -match "([a-fA-F0-9:]+:[a-fA-F0-9:]+)") {
                Write-Host "  Found AAAA record: $address"
                $record.AAAARecord = $address
            }
        } #>
    }
	
	Write-Host "  Performing www nslookup..."
    # Perform nslookup for A and AAAA records
    $nslookupResults = nslookup www.$domain
	$nslookupResults = $nslookupResults | Select-Object -Skip 2

    # Extract A and AAAA records
    foreach ($line in $nslookupResults) {
		Write-Host "Debug output"
		Write-Host $line
        if ($line -match "(\d+\.\d+\.\d+\.\d+)") {
			$address = $matches[1]
			Write-Host "  Found A record: $address"
			$record.WARecord = $address
		} elseif ($line -match "([a-fA-F0-9:]+:[a-fA-F0-9:]+)") {
			$address = $matches[1]
			Write-Host "  Found AAAA record: $address"
			$record.WAAAARecord = $address
		}
		
<#         if ($line -match "Address: (.*)") {
            $address = $matches[1]
            # Determine if the address is IPv4 (A record) or IPv6 (AAAA record)
            if ($address -match "\d+\.\d+\.\d+\.\d+") {
                Write-Host "  Found A record: $address"
                $record.ARecord = $address
            } elseif ($address -match "([a-fA-F0-9:]+:[a-fA-F0-9:]+)") {
                Write-Host "  Found AAAA record: $address"
                $record.AAAARecord = $address
            }
        } #>
    }

    # Add the record to the results array
    $results += $record
}

Write-Host "Exporting results to CSV..."
# Export the results to a CSV file
$results | Export-Csv -NoTypeInformation -Path $outputCsvPath

Write-Host "Script completed. Check $outputCsvPath for output."

# End of script

be sure to adjust the path in the line $domainListPath = “C:\your\path\domains.txt”.

The script can be saved as a .ps1 file, and then executed in the Windows PowerShell.

domains.txt looks like this, for example – simply one domain on each line:

7vi.de
7voice.de
airtaxi.to

The output looks like this:

Did ChatGPT do this on it’s own?

No it did not – but it provided enough syntax and “how-to” for me, to be able to fix the problems quickly. In the comments you can see it’s original code, which took the NS server which I use.

This is an example of how to massively up your productivity and do new things, which you were not able to do before – because the time/output tradeoff would not allow you to.

This is an example of how to run checks and things you need, by spinning up your own scripts quickly.

(If I would have really wanted to do this in the old days, I would probably have used Ruby or Python, and written this on a Linux machine. My desktop happens to be – still – a Windows machine, so this is the convenient get it up quickly option here.)