Exporting Domain Controller Details to HTML: A Step-by-Step Guide

Exporting Domain Controller Details to HTML: A Step-by-Step Guide
#*****************************************************************
#
#   Script Name:  DomainControllerDetailsToHTML.ps1
#   Version:  1.0
#   Author:  Umesh Kumar
#   Website: waytoinfotech.com
#   More info: Export Domain Controller details to HMTL. Also send same details to mail
#   Description:  Export DCS details 
#   
#***************************************************************** 
# Import AD module
Import-Module ActiveDirectory
 
# Get your ad domain
$DomainName = (Get-ADDomain).DNSRoot
 
# Setup email parameters
$subject = "Domain Controllers in $DomainName"
$priority = "Normal"
$smtpServer = "smtp.waytoinfotech.com"
$emailFrom = "NoReply@waytoinfotech.com"
$emailTo = (Get-ADUser -Identity $ENV:Username -Properties mail).mail
$port = 25
 
# Get all DC's
$AllDCs = Get-ADDomainController -Filter * -Server $DomainName | Select Hostname,Ipv4address,isGlobalCatalog,Site,Forest,OperatingSystem,OperatingSystemHotfix,@{Name="OperationMasterRoles";Expression={$_.OperationMasterRoles -join ";"}}
  
# Create empty DataTable object
$DCTable = New-Object System.Data.DataTable
      
# Add columns
$DCTable.Columns.Add() | Out-Null
$DCTable.Columns[0].Caption = "Hostname"
$DCTable.Columns[0].ColumnName = "Hostname"
  
$DCTable.Columns.Add() | Out-Null
$DCTable.Columns[1].Caption = "IPv4Address"
$DCTable.Columns[1].ColumnName = "IPv4Address"
                      
$DCTable.Columns.Add() | Out-Null
$DCTable.Columns[2].Caption = "isGlobalCatalog"
$DCTable.Columns[2].ColumnName = "isGlobalCatalog"
$DCTable.Columns[2].DataType = "Boolean"
  
$DCTable.Columns.Add() | Out-Null
$DCTable.Columns[3].Caption = "Site"
$DCTable.Columns[3].ColumnName = "Site"
  
$DCTable.Columns.Add() | Out-Null
$DCTable.Columns[4].Caption = "Forest"
$DCTable.Columns[4].ColumnName = "Forest"
  
$DCTable.Columns.Add() | Out-Null
$DCTable.Columns[5].Caption = "OperatingSystem"
$DCTable.Columns[5].ColumnName = "OperatingSystem"
$DCTable.Columns.Add() | Out-Null
$DCTable.Columns[6].Caption = "OperatingSystemHotfix"
$DCTable.Columns[6].ColumnName = "OperatingSystemHotfix"
$DCTable.Columns.Add() | Out-Null
$DCTable.Columns[7].Caption = "OperationMasterRoles"
$DCTable.Columns[7].ColumnName = "OperationMasterRoles"

$DCTable.Columns.Add() | Out-Null
$DCTable.Columns[8].Caption = "PingStatus"
$DCTable.Columns[8].ColumnName = "PingStatus"
 
# Loop each DC                        
ForEach($DC in $AllDCs)
{  
    $ping = ping $DC.Hostname -n 1 | Where-Object {$_ -match "Reply" -or $_ -match "Request timed out" -or $_ -match "Destination host unreachable"}
 
    switch ($ping)
    {
        {$_ -like "Reply*" }                          { $PingStatus = "Success" }
        {$_ -like "Request timed out*"}               { $PingStatus = "Timeout" }
        {$_ -like "Destination host unreachable*"}    { $PingStatus = "Unreachable" }
        default                                       { $PingStatus = "Unknown" }
    }
          
    $DCTable.Rows.Add(  $DC.Hostname,
                        $DC.Ipv4address,
                        $DC.isGlobalCatalog,
                        $DC.Site,
                        $DC.Forest,
                        $DC.OperatingSystem,
                        $DC.OperatingSystemHotfix,
                        $DC.OperationMasterRoles,
                        $PingStatus
                              
                        )| Out-Null                          
}
 
# Display results in console 
$DCTable | Sort-Object Site | Format-Table
 
#Creating head style
$Head = @"

"@
 
 
# Email body
[string]$body = [PSCustomObject]$DCTable | Select-Object Hostname,Ipv4address,isGlobalCatalog,Site,Forest,OperatingSystem,OperatingSystemHotfix,OperationMasterRoles,PingStatus | Sort-Object -Property Site | ConvertTo-HTML -Head $head -Body "
Domain Controllers
"
 
$body | Out-File -FilePath c:\script\DCsDetails.htm
# Send the report email
Send-MailMessage -To $emailTo -Subject $subject -BodyAsHtml $body  -SmtpServer $smtpServer -Port $port -From $emailFrom -Priority $priority
 

Share :

Add New Comment

 Your Comment has been sent successfully. Thank you!   Refresh
Error: Please try again