r/ScriptSwap • u/grberk • Oct 05 '23
Looking for assistance with a script
I have a script I cobbled together to query DNS for A records and CNAME records, ping all of the host names, and then dump the results into a CSV file. This is working OK, but I want to take it up a step and have it export to an Excel spreadsheet and also color code hosts that are online as green and and offline as red. I can open the CSV file in Excel and add formatting rules to accomplish this, but I really want to just automate this when the file gets created. However, I'm having issues getting the script to work exporting to a spreadsheet. Here is what I have to export to a CSV:
# Define the DNS server to query
$dnsServer = "IP.ADD.RESS"
# Define the output CSV file
$outputFile = "DNS_Ping_Results.csv"
# Create an empty array to store the results
$results = @()
# Query A records and CNAME records from DNS
$dnsARecords = Get-DnsServerResourceRecord -ComputerName $dnsServer -ZoneName "domain.com" -RRType A
$dnsCNAMERecords = Get-DnsServerResourceRecord -ComputerName $dnsServer -ZoneName "domain.com" -RRType CNAME
# Function to ping an address and return the result
function Ping-Address($address) {
$pingResult = Test-Connection -ComputerName $address -Count 1 -ErrorAction SilentlyContinue
if ($pingResult) {
return "Online"
} else {
return "Offline"
}
}
# Loop through A records and ping each address
foreach ($record in $dnsARecords) {
$recordObject = New-Object PSObject -Property @{
"RecordType" = "A"
"HostName" = $record.HostName
"IPAddress" = $record.RecordData.IPv4Address.IPAddressToString
"PingStatus" = (Ping-Address -address $record.RecordData.IPv4Address.IPAddressToString)
}
$results += $recordObject
}
# Loop through CNAME records, resolve to A records, and ping each address
foreach ($record in $dnsCNAMERecords) {
$cname = $record.RecordData.HostNameAlias
$resolvedARecord = Resolve-DnsName -Name $cname | Where-Object { $_.QueryType -eq "A" } | Select-Object -First 1
if ($resolvedARecord) {
$recordObject = New-Object PSObject -Property @{
"RecordType" = "CNAME"
"HostName" = $record.HostName
"CNAME" = $cname
"IPAddress" = $resolvedARecord.IPAddress
"PingStatus" = (Ping-Address -address $resolvedARecord.IPAddress)
}
$results += $recordObject
}
}
# Export the results to a CSV file
$results | Export-Csv -Path $outputFile -NoTypeInformation
Write-Host "DNS query and ping results exported to $outputFile"
Can any of you assist on the end part and get it to work with exporting to Excel with formatting rules?
View all comments
Show parent comment
2
u/grberk Oct 06 '23
Interestingly enough, I did try to get a working script from ChatGPT for this, and I generated over 9 different versions of one and none of them worked for one reason or another. I'm trying out what your link has and will see if it works or not. It's dedinitely different than what my attempts came up with. I have a crap ton of IPs in my DNS, so this will take a while before I know if it works right or not. I'll follow up soon. Thanks.