Add Powershell/dns-zone-file-to-csv
parent
bc23f58d4b
commit
34f3bf7167
1 changed files with 54 additions and 0 deletions
54
Powershell%2Fdns-zone-file-to-csv.-.md
Normal file
54
Powershell%2Fdns-zone-file-to-csv.-.md
Normal file
|
@ -0,0 +1,54 @@
|
||||||
|
```powershell
|
||||||
|
# .\ZoneToCsv.ps1 -InputPath .\zones.txt -OutputPath .\zones.csv
|
||||||
|
# ZoneToCsv.ps1
|
||||||
|
param(
|
||||||
|
[Parameter(Mandatory)] [string] $InputPath,
|
||||||
|
[Parameter(Mandatory)] [string] $OutputPath
|
||||||
|
)
|
||||||
|
|
||||||
|
$records = New-Object System.Collections.Generic.List[object]
|
||||||
|
$currentZone = ''
|
||||||
|
|
||||||
|
Get-Content -LiteralPath $InputPath | ForEach-Object {
|
||||||
|
$line = $_.Trim()
|
||||||
|
if ([string]::IsNullOrWhiteSpace($line)) { return }
|
||||||
|
if ($line.StartsWith(';') -or $line.StartsWith('#')) { return } # comments
|
||||||
|
if ($line.StartsWith('$')) { return } # directives ($ORIGIN, $TTL, etc.)
|
||||||
|
|
||||||
|
# zone header like "example.com:"
|
||||||
|
if ($line -match '^\s*([A-Za-z0-9\.\-\*]+):\s*$') {
|
||||||
|
$currentZone = $matches[1].TrimEnd('.')
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
# normalize whitespace, then parse
|
||||||
|
$parts = -split ($line -replace '\s+', ' ')
|
||||||
|
if ($parts.Length -lt 3) { return }
|
||||||
|
|
||||||
|
$i = 0
|
||||||
|
$name = $parts[$i++].TrimEnd('.')
|
||||||
|
|
||||||
|
$ttl = ''
|
||||||
|
if ($i -lt $parts.Length -and $parts[$i] -match '^\d+$') { $ttl = $parts[$i++]; }
|
||||||
|
|
||||||
|
$class = ''
|
||||||
|
if ($i -lt $parts.Length -and $parts[$i] -match '^(IN|CH|HS)$') { $class = $parts[$i++]; }
|
||||||
|
|
||||||
|
if ($i -ge $parts.Length) { return }
|
||||||
|
$type = $parts[$i++]
|
||||||
|
|
||||||
|
$rdata = ($parts[$i..($parts.Length-1)] -join ' ')
|
||||||
|
|
||||||
|
$records.Add([pscustomobject]@{
|
||||||
|
Zone = $currentZone
|
||||||
|
Name = $name
|
||||||
|
TTL = $ttl
|
||||||
|
Class = $class
|
||||||
|
Type = $type
|
||||||
|
RData = $rdata
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
$records | Export-Csv -NoTypeInformation -Encoding UTF8 -Path $OutputPath
|
||||||
|
Write-Host "Wrote $($records.Count) records to $OutputPath"
|
||||||
|
```
|
Loading…
Add table
Add a link
Reference in a new issue