diff --git a/Powershell%2Fdns-zone-file-to-csv.-.md b/Powershell%2Fdns-zone-file-to-csv.-.md new file mode 100644 index 0000000..1c677d7 --- /dev/null +++ b/Powershell%2Fdns-zone-file-to-csv.-.md @@ -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" +``` \ No newline at end of file