Domain Controller Monitoring with PowerShell using 0365 SMTP rely

Copper Contributor

Dear Techies,

 

 I am trying to monitor my domain controllers using below powershell script. However, I am unable to associate 0365 SMTP rely server in the script. Since I am not very good at powershell.. It would be really appreciate if someone could assist to resolve this issue.

 

The script is attached in the post in txt format.

 

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

#############################################################################
#       Author: Khalid Ahmed
#       Reviewer:   
#       Date: 08/08/2016
#       Satus: Ping,Netlogon,NTDS,DNS,DCdiag Test(Replication,sysvol,Services)
#       Update: Added Advertising
#       Description: AD Health Status
#############################################################################
###########################Define Variables##################################

$reportpath = ".\Etisalat AD Health Check Report.htm"

if((test-path $reportpath) -like $false)
{
new-item $reportpath -type file
}
$smtphost = "smtp.office365.com"
$from = "DoNotReply@abc.com"
$To = "khahamed@abc.com"
$To1 = "mankhan@abc.com"
$To2 = "ssadiq@abc.com"
$To3 = "mqayed@abc.com"
$To4 = "mohammansari@abc.com"
$To5 = "jqazi@abc.com"
$To6 = "asnoman@abc.com"
$To7 = "bamairi@abc.com"
$To8 = "mjamal@abc.com"
$CC = "azeemsk@abc.com"
$File = "C:\Script\DC Health check\HOENG DC Disk Space Report.xlsx"
$File1 = "C:\Script\DC Health check\Etisalat AD Health Check Report.htm"
$File2 = "C:\Script\DC Health check\ReplicationSummary.csv"
$timeout = "60"

###############################HTml Report Content############################
$report = $reportpath

Clear-Content $report
Add-Content $report "<html>"
Add-Content $report "<head>"
Add-Content $report "<meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1'>"
Add-Content $report '<title>AD Status Report</title>'
add-content $report '<STYLE TYPE="text/css">'
add-content $report  "<!--"
add-content $report  "td {"
add-content $report  "font-family: Tahoma;"
add-content $report  "font-size: 11px;"
add-content $report  "border-top: 1px solid #999999;"
add-content $report  "border-right: 1px solid #999999;"
add-content $report  "border-bottom: 1px solid #999999;"
add-content $report  "border-left: 1px solid #999999;"
add-content $report  "padding-top: 0px;"
add-content $report  "padding-right: 0px;"
add-content $report  "padding-bottom: 0px;"
add-content $report  "padding-left: 0px;"
add-content $report  "}"
add-content $report  "body {"
add-content $report  "margin-left: 5px;"
add-content $report  "margin-top: 5px;"
add-content $report  "margin-right: 0px;"
add-content $report  "margin-bottom: 10px;"
add-content $report  ""
add-content $report  "table {"
add-content $report  "border: thin solid #000000;"
add-content $report  "}"
add-content $report  "-->"
add-content $report  "</style>"
Add-Content $report "</head>"
Add-Content $report "<body>"
add-content $report  "<table width='100%'>"
add-content $report  "<tr bgcolor='Lavender'>"
add-content $report  "<td colspan='7' height='25' align='center'>"
add-content $report  "<font face='tahoma' color='#003399' size='4'><strong>Etisalat - Active Directory Health Check</strong></font>"
add-content $report  "</td>"
add-content $report  "</tr>"
add-content $report  "</table>"
 
add-content $report  "<table width='100%'>"
Add-Content $report  "<tr bgcolor='LightSkyBlue'>"
Add-Content $report  "<td width='14%' align='center'><B>Domain Controller</B></td>"
Add-Content $report  "<td width='6%' align='center'><B>Ping Status</B></td>"
Add-Content $report  "<td width='8%' align='center'><B>Up time status Report</B></td>"
Add-Content $report  "<td width='5%' align='center'><B>Last Boot Time</B></td>"
Add-Content $report  "<td width='5%' align='center'><B>C Drive free Space (GB)</B></td>"
Add-Content $report  "<td width='6%' align='center'><B>Netlogon Service</B></td>"
Add-Content $report  "<td width='6%' align='center'><B>NTDS Service</B></td>"
Add-Content $report  "<td width='6%' align='center'><B>DNS Service Status</B></td>"
Add-Content $report  "<td width='6%' align='center'><B>Netlogons Test</B></td>"
Add-Content $report  "<td width='6%' align='center'><B>Replication Test</B></td>"
Add-Content $report  "<td width='6%' align='center'><B>Services Test</B></td>"
Add-Content $report  "<td width='6%' align='center'><B>Advertising Test</B></td>"
Add-Content $report  "<td width='6%' align='center'><B>FSMO Check Test</B></td>"
Add-Content $report  "<td width='6%' align='center'><B>KCC Check Test</B></td>"
Add-Content $report  "<td width='6%' align='center'><B>FRSO Check Test</B></td>"

 
Add-Content $report "</tr>"

#####################################Get ALL DC Servers#################################
$getForest = [system.directoryservices.activedirectory.Forest]::GetCurrentForest()

$DCServers = $getForest.domains | ForEach-Object {$_.DomainControllers} | ForEach-Object {$_.Name}


################Ping Test######

foreach ($DC in $DCServers){
$Identity = $DC
                Add-Content $report "<tr>"
if ( Test-Connection -ComputerName $DC -Count 1 -ErrorAction SilentlyContinue ) {
Write-Host $DC `t $DC `t Ping Success -ForegroundColor Green
 
  Add-Content $report "<td bgcolor= 'GainsBoro' align=center>  <B> $Identity</B></td>"
                Add-Content $report "<td bgcolor= 'LimeGreen' align=center>  <B>Success</B></td>"
        #####################Uptime############################
               $wmi = 0
               $upt=0
               $Lastb =0
               $wmijob = start-job -scriptblock {Get-WmiObject -ComputerName $($args[0]) -Query "SELECT LastBootUpTime FROM Win32_OperatingSystem"} -ArgumentList $DC
               wait-job $wmijob -timeout $timeout
               if($wmijob.state -like "Running")
               {
               Write-Host $DC `t Uptime Test TimeOut -ForegroundColor Yellow
               Add-Content $report "<td bgcolor= 'Yellow' align=center><B>UptimeTimeout</B></td>"
               Add-Content $report "<td bgcolor= 'Yellow' align=center><B>UptimeTimeout</B></td>"
               stop-job $wmijob
               }
               else
               {
               $wmi = Receive-job $wmijob
               $now = 0
             $now = Get-Date
            $boottime = 0
               $boottime = [System.Management.ManagementDateTimeConverter]::ToDateTime($wmi.LastBootUpTime)
               $uptime = 0
               $uptime = $now - $boottime
               $d = 0
               $d =$uptime.days
               $h = 0
               $h =$uptime.hours
               $m = 0
               $m =$uptime.Minutes
            $s = 0
               $s = $uptime.Seconds
               $upt = "$d Days $h Hours $m Min $s Sec"
               $Lastb = $boottime
               Write-Host $DC `t uptime $upt Lastboot $Lastb   -ForegroundColor Green
               Add-Content $report "<td bgcolor= 'Aquamarine' align=center><B>$upt</B></td>"
               Add-Content $report "<td bgcolor= 'Aquamarine' align=center><B>$Lastb</B></td>"
               }
        #####################Crive utilization############################
               $wmi = 0
        $cdriv = 0
               $wmijob = start-job -scriptblock {Get-WmiObject win32_logicaldisk -ComputerName $($args[0]) |  Where-Object {$_.drivetype -eq 3}} -ArgumentList $DC
               wait-job $wmijob -timeout $timeout
               if($wmijob.state -like "Running")
               {
               Write-Host $DC `t Uptime Test TimeOut -ForegroundColor Yellow
               Add-Content $report "<td bgcolor= 'Yellow' align=center><B>FspaceTimeout</B></td>"
               stop-job $wmijob
               }
               else
               {
               $wmi = Receive-job $wmijob
               $cdriv = $wmi | where{$_.DeviceID -eq "C:"}
               $fspace = [math]::truncate($cdriv.Freespace/1Gb)
               Write-Host $DC `t c drive Free Space $fspace   -ForegroundColor Green
               Add-Content $report "<td bgcolor= 'Aquamarine' align=center><B>$fspace</B></td>"
        }
                ##############Netlogon Service Status################
  $serviceStatus = start-job -scriptblock {get-service -ComputerName $($args[0]) -Name "Netlogon" -ErrorAction SilentlyContinue} -ArgumentList $DC
                wait-job $serviceStatus -timeout $timeout
                if($serviceStatus.state -like "Running")
                {
                 Write-Host $DC `t Netlogon Service TimeOut -ForegroundColor Yellow
                 Add-Content $report "<td bgcolor= 'Yellow' align=center><B>NetlogonTimeout</B></td>"
                 stop-job $serviceStatus
                }
                else
                {
                $serviceStatus1 = Receive-job $serviceStatus
                 if ($serviceStatus1.status -eq "Running") {
      Write-Host $DC `t $serviceStatus1.name `t $serviceStatus1.status -ForegroundColor Green
             $svcName = $serviceStatus1.name
             $svcState = $serviceStatus1.status         
             Add-Content $report "<td bgcolor= 'LimeGreen' align=center><B>$svcState</B></td>"
                  }
                 else
                  {
           Write-Host $DC `t $serviceStatus1.name `t $serviceStatus1.status -ForegroundColor Red
            $svcName = $serviceStatus1.name
            $svcState = $serviceStatus1.status         
            Add-Content $report "<td bgcolor= 'Red' align=center><B>$svcState</B></td>"
                  }
                }
               ######################################################
                ##############NTDS Service Status################
  $serviceStatus = start-job -scriptblock {get-service -ComputerName $($args[0]) -Name "NTDS" -ErrorAction SilentlyContinue} -ArgumentList $DC
                wait-job $serviceStatus -timeout $timeout
                if($serviceStatus.state -like "Running")
                {
                 Write-Host $DC `t NTDS Service TimeOut -ForegroundColor Yellow
                 Add-Content $report "<td bgcolor= 'Yellow' align=center><B>NTDSTimeout</B></td>"
                 stop-job $serviceStatus
                }
                else
                {
                $serviceStatus1 = Receive-job $serviceStatus
                 if ($serviceStatus1.status -eq "Running") {
      Write-Host $DC `t $serviceStatus1.name `t $serviceStatus1.status -ForegroundColor Green
             $svcName = $serviceStatus1.name
             $svcState = $serviceStatus1.status         
             Add-Content $report "<td bgcolor= 'LimeGreen' align=center><B>$svcState</B></td>"
                  }
                 else
                  {
           Write-Host $DC `t $serviceStatus1.name `t $serviceStatus1.status -ForegroundColor Red
            $svcName = $serviceStatus1.name
            $svcState = $serviceStatus1.status         
            Add-Content $report "<td bgcolor= 'Red' align=center><B>$svcState</B></td>"
                  }
                }
               ######################################################
                ##############DNS Service Status################
  $serviceStatus = start-job -scriptblock {get-service -ComputerName $($args[0]) -Name "DNS" -ErrorAction SilentlyContinue} -ArgumentList $DC
                wait-job $serviceStatus -timeout $timeout
                if($serviceStatus.state -like "Running")
                {
                 Write-Host $DC `t DNS Server Service TimeOut -ForegroundColor Yellow
                 Add-Content $report "<td bgcolor= 'Yellow' align=center><B>DNSTimeout</B></td>"
                 stop-job $serviceStatus
                }
                else
                {
                $serviceStatus1 = Receive-job $serviceStatus
                 if ($serviceStatus1.status -eq "Running") {
      Write-Host $DC `t $serviceStatus1.name `t $serviceStatus1.status -ForegroundColor Green
             $svcName = $serviceStatus1.name
             $svcState = $serviceStatus1.status         
             Add-Content $report "<td bgcolor= 'LimeGreen' align=center><B>$svcState</B></td>"
                  }
                 else
                  {
           Write-Host $DC `t $serviceStatus1.name `t $serviceStatus1.status -ForegroundColor Red
            $svcName = $serviceStatus1.name
            $svcState = $serviceStatus1.status         
            Add-Content $report "<td bgcolor= 'Red' align=center><B>$svcState</B></td>"
                  }
                }
               ######################################################

               ####################Netlogons status##################
               add-type -AssemblyName microsoft.visualbasic
               $cmp = "microsoft.visualbasic.strings" -as [type]
               $sysvol = start-job -scriptblock {dcdiag /test:netlogons /s:$($args[0])} -ArgumentList $DC
               wait-job $sysvol -timeout $timeout
               if($sysvol.state -like "Running")
               {
               Write-Host $DC `t Netlogons Test TimeOut -ForegroundColor Yellow
               Add-Content $report "<td bgcolor= 'Yellow' align=center><B>NetlogonsTimeout</B></td>"
               stop-job $sysvol
               }
               else
               {
               $sysvol1 = Receive-job $sysvol
               if($cmp::instr($sysvol1, "passed test NetLogons"))
                  {
                  Write-Host $DC `t Netlogons Test passed -ForegroundColor Green
                  Add-Content $report "<td bgcolor= 'LimeGreen' align=center><B>Netlogons Passed</B></td>"
                  }
               else
                  {
                  Write-Host $DC `t Netlogons Test Failed -ForegroundColor Red
                  Add-Content $report "<td bgcolor= 'Red' align=center><B>NetlogonsFail</B></td>"
                  }
                }
               ########################################################
               ####################Replications status##################
               add-type -AssemblyName microsoft.visualbasic
               $cmp = "microsoft.visualbasic.strings" -as [type]
               $sysvol = start-job -scriptblock {dcdiag /test:Replications /s:$($args[0])} -ArgumentList $DC
               wait-job $sysvol -timeout $timeout
               if($sysvol.state -like "Running")
               {
               Write-Host $DC `t Replications Test TimeOut -ForegroundColor Yellow
               Add-Content $report "<td bgcolor= 'Yellow' align=center><B>ReplicationsTimeout</B></td>"
               stop-job $sysvol
               }
               else
               {
               $sysvol1 = Receive-job $sysvol
               if($cmp::instr($sysvol1, "passed test Replications"))
                  {
                  Write-Host $DC `t Replications Test passed -ForegroundColor Green
                  Add-Content $report "<td bgcolor= 'LimeGreen' align=center><B>Replications Passed</B></td>"
                  }
               else
                  {
                  Write-Host $DC `t Replications Test Failed -ForegroundColor Red
                  Add-Content $report "<td bgcolor= 'Red' align=center><B>ReplicationsFail</B></td>"
                  }
                }
               ########################################################
        ####################Services status##################
               add-type -AssemblyName microsoft.visualbasic
               $cmp = "microsoft.visualbasic.strings" -as [type]
               $sysvol = start-job -scriptblock {dcdiag /test:Services /s:$($args[0])} -ArgumentList $DC
               wait-job $sysvol -timeout $timeout
               if($sysvol.state -like "Running")
               {
               Write-Host $DC `t Services Test TimeOut -ForegroundColor Yellow
               Add-Content $report "<td bgcolor= 'Yellow' align=center><B>ServicesTimeout</B></td>"
               stop-job $sysvol
               }
               else
               {
               $sysvol1 = Receive-job $sysvol
               if($cmp::instr($sysvol1, "passed test Services"))
                  {
                  Write-Host $DC `t Services Test passed -ForegroundColor Green
                  Add-Content $report "<td bgcolor= 'LimeGreen' align=center><B>Services Passed</B></td>"
                  }
               else
                  {
                  Write-Host $DC `t Services Test Failed -ForegroundColor Red
                  Add-Content $report "<td bgcolor= 'Red' align=center><B>ServicesFail</B></td>"
                  }
                }
               ########################################################
        ####################Advertising status##################
               add-type -AssemblyName microsoft.visualbasic
               $cmp = "microsoft.visualbasic.strings" -as [type]
               $sysvol = start-job -scriptblock {dcdiag /test:Advertising /s:$($args[0])} -ArgumentList $DC
               wait-job $sysvol -timeout $timeout
               if($sysvol.state -like "Running")
               {
               Write-Host $DC `t Advertising Test TimeOut -ForegroundColor Yellow
               Add-Content $report "<td bgcolor= 'Yellow' align=center><B>AdvertisingTimeout</B></td>"
               stop-job $sysvol
               }
               else
               {
               $sysvol1 = Receive-job $sysvol
               if($cmp::instr($sysvol1, "passed test Advertising"))
                  {
                  Write-Host $DC `t Advertising Test passed -ForegroundColor Green
                  Add-Content $report "<td bgcolor= 'LimeGreen' align=center><B>Advertising Passed</B></td>"
                  }
               else
                  {
                  Write-Host $DC `t Advertising Test Failed -ForegroundColor Red
                  Add-Content $report "<td bgcolor= 'Red' align=center><B>AdvertisingFail</B></td>"
                  }
                }
               ########################################################
        ####################FSMOCheck status##################
               add-type -AssemblyName microsoft.visualbasic
               $cmp = "microsoft.visualbasic.strings" -as [type]
               $sysvol = start-job -scriptblock {dcdiag /test:FSMOCheck /s:$($args[0])} -ArgumentList $DC
               wait-job $sysvol -timeout $timeout
               if($sysvol.state -like "Running")
               {
               Write-Host $DC `t FSMOCheck Test TimeOut -ForegroundColor Yellow
               Add-Content $report "<td bgcolor= 'Yellow' align=center><B>FSMOCheckTimeout</B></td>"
               stop-job $sysvol
               }
               else
               {
               $sysvol1 = Receive-job $sysvol
               if($cmp::instr($sysvol1, "passed test FsmoCheck"))
                  {
                  Write-Host $DC `t FSMOCheck Test passed -ForegroundColor Green
                  Add-Content $report "<td bgcolor= 'LimeGreen' align=center><B>FSMO Check Passed</B></td>"
                  }
               else
                  {
                  Write-Host $DC `t FSMOCheck Test Failed -ForegroundColor Red
                  Add-Content $report "<td bgcolor= 'Red' align=center><B>FSMOCheckFail</B></td>"
                  }
                }
               ########################################################         
        ####################KCCheck status##################
               add-type -AssemblyName microsoft.visualbasic
               $cmp = "microsoft.visualbasic.strings" -as [type]
               $sysvol = start-job -scriptblock {dcdiag /test:KccEvent /s:$($args[0])} -ArgumentList $DC
               wait-job $sysvol -timeout $timeout
               if($sysvol.state -like "Running")
               {
               Write-Host $DC `t KccEvent Test TimeOut -ForegroundColor Yellow
               Add-Content $report "<td bgcolor= 'Yellow' align=center><B>KccEventTimeout</B></td>"
               stop-job $sysvol
               }
               else
               {
               $sysvol1 = Receive-job $sysvol
               if($cmp::instr($sysvol1, "passed test KccEvent"))
                  {
                  Write-Host $DC `t KccEvent Test passed -ForegroundColor Green
                  Add-Content $report "<td bgcolor= 'LimeGreen' align=center><B>Kcc Event Passed</B></td>"
                  }
               else
                  {
                  Write-Host $DC `t KccEvent Test Failed -ForegroundColor Red
                  Add-Content $report "<td bgcolor= 'Red' align=center><B>KccEventFail</B></td>"
                  }
                }
               ########################################################
        ####################FrsEvent status##################
               add-type -AssemblyName microsoft.visualbasic
               $cmp = "microsoft.visualbasic.strings" -as [type]
               $sysvol = start-job -scriptblock {dcdiag /test:FrsEvent /s:$($args[0])} -ArgumentList $DC
               wait-job $sysvol -timeout $timeout
               if($sysvol.state -like "Running")
               {
               Write-Host $DC `t FrsEvent Test TimeOut -ForegroundColor Yellow
               Add-Content $report "<td bgcolor= 'Yellow' align=center><B>FrsEventTimeout</B></td>"
               stop-job $sysvol
               }
               else
               {
               $sysvol1 = Receive-job $sysvol
               if($cmp::instr($sysvol1, "passed test FrsEvent"))
                  {
                  Write-Host $DC `t FrsEvent Test passed -ForegroundColor Green
                  Add-Content $report "<td bgcolor= 'LimeGreen' align=center><B>Frs Event Passed</B></td>"
                  }
               else
                  {
                  Write-Host $DC `t FrsEvent Test Failed -ForegroundColor Red
                  Add-Content $report "<td bgcolor= 'Red' align=center><B>FrsEventFail</B></td>"
                  }
                }

                                     
}
else
              {
Write-Host $DC `t $DC `t Ping Fail -ForegroundColor Red
  Add-Content $report "<td bgcolor= 'GainsBoro' align=center>  <B> $Identity</B></td>"
                Add-Content $report "<td bgcolor= 'Red' align=center>  <B>Ping Fail</B></td>"
  Add-Content $report "<td bgcolor= 'Red' align=center>  <B>Ping Fail</B></td>"
  Add-Content $report "<td bgcolor= 'Red' align=center>  <B>Ping Fail</B></td>"
  Add-Content $report "<td bgcolor= 'Red' align=center>  <B>Ping Fail</B></td>"
  Add-Content $report "<td bgcolor= 'Red' align=center>  <B>Ping Fail</B></td>"
  Add-Content $report "<td bgcolor= 'Red' align=center>  <B>Ping Fail</B></td>"
  Add-Content $report "<td bgcolor= 'Red' align=center>  <B>Ping Fail</B></td>"
  Add-Content $report "<td bgcolor= 'Red' align=center>  <B>Ping Fail</B></td>"
  Add-Content $report "<td bgcolor= 'Red' align=center>  <B>Ping Fail</B></td>"
}        
      
}

Add-Content $report "</tr>"
############################################Close HTMl Tables###########################


Add-content $report  "</table>"
Add-Content $report "</body>"
Add-Content $report "</html>"


########################################################################################
#############################################Send Email#################################
 
 
$subject = "Etisalat AD Health Check Report" 
$body = Get-Content ".\Etisalat AD Health Check Report.htm"
$smtp = New-Object System.Net.Mail.SmtpClient $smtphost 
$msg = New-Object System.Net.Mail.MailMessage $from, $to, $subject, $body
$msg.To.Add($To1)
$msg.To.Add($To2)
$msg.To.Add($To3)
$msg.To.Add($To4)
$msg.To.Add($To5)
$msg.To.Add($To6)
$msg.To.Add($To7)
$msg.To.Add($To8)
$msg.CC.Add($CC)
$att = new-object Net.Mail.Attachment($File) #Attachement
$msg.Attachments.Add($att)
$att1 = new-object Net.Mail.Attachment($File1) #Attachement
$msg.Attachments.Add($att1)
$att2 = new-object Net.Mail.Attachment($File2) #Attachement
$msg.Attachments.Add($att2)
$msg.isBodyhtml = $true 
$smtp.send($msg)
 
########################################################################################
 

 
          
  

3 Replies

You need to authenticate to use the Office 365 service as a relay

 

$SMTP.Credentials = New-Object System.Net.NetworkCredential("username", "password"); 

 

PowerShell requires that a user enter credentials through a dialog to send email.  Some devs have found a way to store creds in a text file and accomplish this action.

 

I have also discovered that creating a MailMessage object using purely strings does not authenticate.  The From entry must be a MailAddress object.  Personally, I think this to be a bug.