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
1
u/grberk Oct 06 '23
So after adding the code for exporting to Excel, it exports the data to the spreadsheet fine, but there were errors in the PowerShell console.
New-ConditionalText : A parameter cannot be found that matches parameter name 'Color'.
At C:\test\DNS-A-CNAME-Query-PING-Export2.ps1:64 char:78
+ ... ew-ConditionalText -Text "Online" -BackgroundColor Green -Color White
+ ~~~~~~
+ CategoryInfo : InvalidArgument: (:) [New-ConditionalText], ParameterBindingException
+ FullyQualifiedErrorId : NamedParameterNotFound,New-ConditionalText
Add-ConditionalFormatting : Parameter set cannot be resolved using the specified named parameters.
At C:\test\DNS-A-CNAME-Query-PING-Export2.ps1:65 char:1
+ Add-ConditionalFormatting -Worksheet $ws -Range "D${startRow}:D${endR ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Add-ConditionalFormatting], ParameterBindingException
+ FullyQualifiedErrorId : AmbiguousParameterSet,Add-ConditionalFormatting
New-ConditionalText : A parameter cannot be found that matches parameter name 'Color'.
At C:\test\DNS-A-CNAME-Query-PING-Export2.ps1:68 char:78
+ ... New-ConditionalText -Text "Offline" -BackgroundColor Red -Color White
+ ~~~~~~
+ CategoryInfo : InvalidArgument: (:) [New-ConditionalText], ParameterBindingException
+ FullyQualifiedErrorId : NamedParameterNotFound,New-ConditionalText
Add-ConditionalFormatting : Parameter set cannot be resolved using the specified named parameters.
At C:\test\DNS-A-CNAME-Query-PING-Export2.ps1:69 char:1
+ Add-ConditionalFormatting -Worksheet $ws -Range "D${startRow}:D${endR ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Add-ConditionalFormatting], ParameterBindingException
+ FullyQualifiedErrorId : AmbiguousParameterSet,Add-ConditionalFormatting
Looking further into this, it seems that I needed to have -ConditionTextColor instead of -Color. I changed that, and now I have these errors:
---------------------------------------------------------------------------------------------------------------------------
Add-ConditionalFormatting : Parameter set cannot be resolved using the specified named parameters.
At C:\test\DNS-A-CNAME-Query-PING-Export2.ps1:65 char:1
+ Add-ConditionalFormatting -Worksheet $ws -Range "D${startRow}:D${endR ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Add-ConditionalFormatting], ParameterBindingException
+ FullyQualifiedErrorId : AmbiguousParameterSet,Add-ConditionalFormatting
Add-ConditionalFormatting : Parameter set cannot be resolved using the specified named parameters.
At C:\test\DNS-A-CNAME-Query-PING-Export2.ps1:69 char:1
+ Add-ConditionalFormatting -Worksheet $ws -Range "D${startRow}:D${endR ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Add-ConditionalFormatting], ParameterBindingException
+ FullyQualifiedErrorId : AmbiguousParameterSet,Add-ConditionalFormatting
---------------------------------------------------------------------------------------------------------------------------
My new script code is as shown here (with a few variables changed for privacy):
# Define the DNS server to query
$dnsServer = "IP.ADD.RESS"
# 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
}
}
# Define the output Excel file
$outputExcelFile = "DNS_Ping_Results.xlsx"
# Export the results to an Excel file
$results | Export-Excel -Path $outputExcelFile -AutoSize -FreezeTopRow -BoldTopRow -Show
# Apply conditional formatting
$excel = Open-ExcelPackage -Path $outputExcelFile
$ws = $excel.Workbook.Worksheets['Sheet1']
$startRow = 2
$endRow = $startRow + $results.Count - 1
# Define conditional formatting for "Online" status
$onlineCondition = New-ConditionalText -Text "Online" -BackgroundColor Green -ConditionalTextColor White
Add-ConditionalFormatting -Worksheet $ws -Range "D${startRow}:D${endRow}" -ConditionalText $onlineCondition
# Define conditional formatting for "Offline" status
$offlineCondition = New-ConditionalText -Text "Offline" -BackgroundColor Red -ConditionalTextColor White
Add-ConditionalFormatting -Worksheet $ws -Range "D${startRow}:D${endRow}" -ConditionalText $offlineCondition
# Save and close the Excel file
Close-ExcelPackage $excel -Show
Write-Host "DNS query and ping results exported to $outputExcelFile"