Advertisement
Guest User

Untitled

a guest
Sep 20th, 2017
474
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.29 KB | None | 0 0
  1. # Name: Email-AccountLockout.ps1
  2. # Author: James Schlackman
  3. # Last Modified: May 18 2017
  4. #
  5. # Automatically emails the appropriate help desk when a user's AD account is locked out.
  6. # Runs on the Domain Controller with the PDC emulator role and triggered by a scheduled task
  7. # attached to event ID 4740 in the Security event log.
  8.  
  9. param(
  10. [string]$username
  11. )
  12.  
  13. Import-Module ActiveDirectory
  14.  
  15. # Set up mail sending parameters
  16. $MailRelay = "smtp.contoso.com"
  17. $Subject = "AD Account Locked Out: $username"
  18. $FromAddress = "$env:COMPUTERNAME <no-reply@contoso.com>"
  19.  
  20. # Determine which Help Desk queue to send this to
  21. $UserDesc = (Get-ADuser -LdapFilter "(samaccountname=$username)" -Properties "description").Description
  22. If ($UserDesc -match "Campus East") {
  23. $ToAddress = "easthelpdesk@contoso.com"
  24. } ElseIf (($UserDesc -match "Campus West") -Or ($UserDesc -match "Campus South")) {
  25. $ToAddress = "southwesthelpdesk@contoso.com"
  26. } Else {
  27. $ToAddress = "helpdesk@contoso.com"
  28. }
  29.  
  30.  
  31. # Set up anonymous credentials so Exchange doesn't choke on the server account credentials
  32. $anonUsername = "anonymous"
  33. $anonPassword = ConvertTo-SecureString -String "anonymous" -AsPlainText -Force
  34. $anonCredentials = New-Object System.Management.Automation.PSCredential($anonUsername,$anonPassword)
  35.  
  36. # Create the body of the email
  37.  
  38. $body = "<html><head><style>body {font-family: Calibri, sans-serif; font-size: 11pt} p.footer {font-size: 9pt; font-style: italic; color: gray}</style></head><body><p>`
  39. The Active Directory account for <strong>$username</strong> has been locked out after too many failed login attempts.`
  40. The affected user will no longer be able to log in to any system that authenticates directly to AD or LDAP until the account is unlocked by an administrator`
  41. or until the lockout expires (normally 1 hour after the initial lockout).</p><br>`
  42. <p>The event log on $env:COMPUTERNAME will have further information: check the Event Viewer under <b>Windows Logs\Security</b> and filter on Event ID <b>4740</b> for more details.</p><br>`
  43. <p class=""footer"">This is a scripted message sent via the Task Scheduler on $env:COMPUTERNAME. Do not reply to this message.</p></body></html>"
  44.  
  45. # Send email notification
  46. Send-MailMessage -SmtpServer $MailRelay -Subject $Subject -From $FromAddress -BodyAsHtml $Body -To $ToAddress -credential $anonCredentials
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement