Account lockout email notification (PowerShell)
Introduction
This script shows how to automatically send an email notification to Administrator when there is a user locked out.
Scenarios
When there is a user locked out, and then sends an email to domain admin. There are many users ask this issue, and this could be done by schedule task, we could also create a script to do the job.
Script
This script contains one advanced functions Send-OSCLockOutUser, you can use this script in the following way:
Step1: Open the script file with Notepad. Scroll down to the end of the script file, and then add the example command which you want to run. Then save the file.
Example: Send-OSCLockOutUser -From user1@Domain.com -To admin1@Domain.com,admin2@Domain.com -SMTPServer smtp.domain.com -UserName user1 -Password Password
Step2: Create a scheduled task to run that PowerShell script. In trigger option, you can do as following.
In action option, you can do as following.
Note the argument "-file" should be with the script path. And about scheduled task, there is a reference.
Use the Windows Task Scheduler to Run a Windows PowerShell Script
Here are some code snippets for your references.
$Event = Get-EventLog -LogName Security -InstanceId 4740 -Newest 1
#Store the newest log into email boy
$EmailBody= $Event.Message + "`r`n`t" + $Event.TimeGenerated
#Email subject
$EmailSubj= "User Account locked out"
#Create SMTP client
$SMTPClient = New-Object Net.Mail.SMTPClient($SmtpServer)
$SMTPClient.EnableSSL = $true
#Get the credetials
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential($UserName, $PassWord);
#Create mailmessage object
$emailMessage = New-Object System.Net.Mail.MailMessage
$emailMessage.From = "$From"
Foreach($EmailTo in $To)
{
$emailMessage.To.Add($EmailTo)
}
$emailMessage.Subject = $EmailSubj
$emailMessage.Body = $EmailBody
#Send email
$SMTPClient.Send($emailMessage)
$Event = Get-EventLog -LogName Security -InstanceId 4740 -Newest 1 #Store the newest log into email boy $EmailBody= $Event.Message + "`r`n`t" + $Event.TimeGenerated #Email subject $EmailSubj= "User Account locked out" #Create SMTP client $SMTPClient = New-Object Net.Mail.SMTPClient($SmtpServer) $SMTPClient.EnableSSL = $true #Get the credetials $SMTPClient.Credentials = New-Object System.Net.NetworkCredential($UserName, $PassWord); #Create mailmessage object $emailMessage = New-Object System.Net.Mail.MailMessage $emailMessage.From = "$From" Foreach($EmailTo in $To) { $emailMessage.To.Add($EmailTo) } $emailMessage.Subject = $EmailSubj $emailMessage.Body = $EmailBody #Send email $SMTPClient.Send($emailMessage)
Windows Server 2008R2 or higher version
Microsoft All-In-One Script Framework is an automation script sample library for IT Professionals. The key value that All-In-One Script Framework is trying to deliver is Scenario-Focused Script Samples driven by IT Pros' real-world pains and needs. The team is monitoring all TechNet forums, IT Pros' support calls to Microsoft, and script requests submitted to TechNet Script Repository. We collect frequently asked IT scenarios, and create script samples to automate the tasks and save some time for IT Pros. The team of All-In-One Script Framework sincerely hope that these customer-driven automation script samples can help our IT community in this script-centric move.