Powershell – Statische DNS A Einträge per E-Mail

Mit diesem Skript und einem geplanten Task, kann man sich die statischen DNS Einträge einer Zone per E-Mail zu senden lassen.

Clear-Host
$PathToReport = "C:\Temp"
$To = "joern.walter@ndsedv.de"
$From = "StaticDNS@ndsedv.de"
$SMTPServer = "10.100.10.15"
$ZoneName = "ndsedv.de"
$DomainController = "dc01"

$CurrentDate = Get-Date -Format "dd.MM.yyyy"

Function Set-AlternatingRows {
    [CmdletBinding()]
         Param(
             [Parameter(Mandatory=$True,ValueFromPipeline=$True)]
             [object[]]$HTMLDocument,

             [Parameter(Mandatory=$True)]
             [string]$CSSEvenClass,

             [Parameter(Mandatory=$True)]
             [string]$CSSOddClass
         )
     Begin {
         $ClassName = $CSSEvenClass
     }
     Process {
         [string]$Line = $HTMLDocument
         $Line = $Line.Replace("<tr>","<tr $ClassName"">")
         If ($ClassName -eq $CSSEvenClass)
         {    $ClassName = $CSSOddClass
         }
         Else
         {    $ClassName = $CSSEvenClass
         }
         $Line = $Line.Replace("<table>","<table width=""20%"">")
         Return $Line
     }
}

$Header = @"
<style>
TABLE {border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}
TH {border-width: 1px;padding: 3px;border-style: solid;border-color: black;background-color: #D8E4FA;}
TD {border-width: 1px;padding: 3px;border-style: solid;border-color: black;}
.odd  { background-color:#ffffff; }
.even { background-color:#dddddd; }
</style>
<title>Statische DNS Eintraege aus der Zone $ZoneName Domain vom $CurrentDate</title>
"@

$Report = Get-DnsServerResourceRecord -ZoneName $ZoneName -ComputerName $DomainController -RRType A | Where Timestamp -eq $Null | Select -Property HostName,RecordType -ExpandProperty RecordData | Sort-Object -Property HostName
$NumberOfRecords = $Report | Measure-Object HostName | Select-Object -Property Count
$Report = $Report | Select HostName,RecordType,IPv4Address |
    ConvertTo-Html -Head $Header -PreContent "<p><h2>Statische DNS Eintraege aus der Zone $ZoneName Domain vom $CurrentDate</h2></p><p><h3>$NumberOfRecords Eintraege erfasst</h3></p>" | 
    Set-AlternatingRows -CSSEvenClass even -CSSOddClass odd
$Report | Out-File $PathToReport\Output_AD_GetListStaticARecords.html
Send-MailMessage -To $To -From $From -Subject "Statische DNS Eintraege aus der Zone $ZoneName Domain vom $CurrentDate - Joern Walter" -Body ($Report | Out-String) -BodyAsHtml -SmtpServer $SMTPServer

Write-Host "Skript Done!" -ForegroundColor Green