PowerShell Code
Forest Domain Controller Discovery
View-only PowerShell code for review.
<#
.SYNOPSIS
This script will pull a list of DNS names with specific prefixes from all DNS zones on the domain its run on.
.DESCRIPTION
This script connects to a DNS server using domain admin credentials checked out from our vault, retrieves all the zones, filters records with specified prefixes, and exports the results to a CSV file.
.EXAMPLE
Get-DNSRecords.ps1
This example runs the script to connect to the DNS server, retrieve zones, filter records with specified prefixes, and export the results to a CSV file named "FilteredDNSRecords.csv".
.EXAMPLE
Get-DNSRecords.ps1 -Credential (Get-Credential)
This example runs the script with specified credentials to connect to the DNS server, retrieve zones, filter records with specified prefixes, and export the results to a CSV file named "FilteredDNSRecords.csv".
.INPUTS
The script does not take any inputs directly, but you can modify the variables at the beginning of the script to change the prefixes to filter.
- Credential: The domain admin credentials to connect to the DNS server. this password must be checked out from our vault.
.OUTPUTS
The script outputs a CSV file named "FilteredDNSRecords.csv" with the filtered DNS records with listed prefixes from the synopses.
.NOTES
This script is used for retrieving and filtering DNS records from a DNS server using specified prefixes.
Ensure you have the necessary permissions to access the DNS server and retrieve records.
The script will export the filtered results to a CSV file named "FilteredDNSRecords.csv".
"Pre#" is, at least in my case, was for the 3 character designation for the server location.
#>
$cred = Get-Credential -Message "Enter Domain Admin credentials"
$script = {
# Get the DNS zones and filter records with listed prefixes
$zones = Get-DnsServerZone
$prefixes = @("Pre1", "Pre2", "Pre3", "Pre4","Pre5")
$results = @()
foreach ($zone in $zones) {
$records = Get-DnsServerResourceRecord -ZoneName $zone.ZoneName
$filtered = $records | Where-Object {
$hostname = $_.HostName
$prefixes | Where-Object { $hostname -like "$_*" }
}
foreach ($record in $filtered) {
$results += [PSCustomObject]@{
ZoneName = $zone.ZoneName
HostName = $record.HostName
RecordType = $record.RecordType
RecordData = $record.RecordData
}
}
}
$results | Export-Csv -Path "FilteredDNSRecords.csv" -NoTypeInformation
}
# Save script block to a temporary file
$tempScriptPath = "$env:TEMP\DnsExportScript.ps1"
$script | Out-File -FilePath $tempScriptPath
# Launch new PowerShell session with elevated credentials
Start-Process powershell.exe -Credential $cred -ArgumentList "-ExecutionPolicy Bypass -File `"$tempScriptPath`""