So we've been using this PS script for a while and was working fine until we migrated the domain controllers. The new domain controllers are running core Microsoft Windows Server 2019 Datacenter. We have 2 domain controllers and both are enabled in Task Scheduler. But for some reason the script is working intermittently. Sometimes it sends out the alerts other times it doesn't. And I can't figure out what the issue is exactly.
This is the script I have:
#################################################################
#Declare variables to be used for the Email
$MailSubject= “User Account locked out”
$MailFrom=”[email protected]”
$MailTo=”[email protected]”
#Gets the Event Log that contains the most recent lockout event
$Event = Get-EventLog -LogName Security -InstanceId 4740 -Newest 1
#Creates a variable which contains the contents of the lockout event log. This is used for the actual message in the email
$MailBody= $Event.Message + “`r`n`t” + $Event.TimeGenerated
#Creates an SMTP Object and assigns an SMTP Address
$SmtpClient = New-Object system.net.mail.smtpClient
$SmtpClient.host = “our-adconnect.ourdomain.local”
#Creates a new Mail Message Object. This is the object needed for the addressing email, subject, body, etc
$MailMessage = New-Object system.net.mail.mailmessage
$MailMessage.from = $MailFrom
$MailMessage.To.add($MailTo)
$MailMessage.IsBodyHtml = 0
$MailMessage.Subject = $MailSubject
$MailMessage.Body = $MailBody
#Actually Sends the Message
$SmtpClient.Send($MailMessage)