Link to home
Start Free TrialLog in
Avatar of David Klein
David Klein

asked on

Adding user account name to subject line in account lockout powershell script

using the following script to generate account lock out events but can't seem to find a way to include the user account name in the subject line.  any help would be appreciated

$Report= "c:\scripts\lockedout.html"

$HTML=@"
<title>Account locked out Report</title>
<!--mce:0-->
"@

$Account_Name = @{n='Account name';e={$_.ReplacementStrings[-1]}}
$Account_domain = @{n='Account Domain';e={$_.ReplacementStrings[-2]}}
$Caller_Computer_Name = @{n='Caller Computer Name';e={$_.ReplacementStrings[-1]}}

           
$event= Get-EventLog -LogName Security -InstanceId 4740 -Newest 1 |
   Select TimeGenerated,ReplacementStrings,"Account name","Account Domain","Caller Computer Name" |
   % {
     New-Object PSObject -Property @{
      "Account name" = $_.ReplacementStrings[-7]
      "Account Domain" = $_.ReplacementStrings[5]
      "Caller Computer Name" = $_.ReplacementStrings[1]
      Date = $_.TimeGenerated
    }
   }
   
  $event | ConvertTo-Html -Property "Account name","Account Domain","Caller Computer Name",Date -head $HTML -body  "<H2> User is locked in the Active Directory</H2>"|
       Out-File $Report -Append

$MailBody= Get-Content $Report
$MailSubject= "User Account Locked Out – MY_Domain\"
$SmtpClient = New-Object system.net.mail.smtpClient
$SmtpClient.host = "mysmtp.mydomain.com"
$MailMessage = New-Object system.net.mail.mailmessage
$MailMessage.from = "User-Account-Lockout@mydomain.com"
$MailMessage.To.add("mydomainaccountlockout@mydomain.com")
$MailMessage.Subject = $MailSubject
$MailMessage.IsBodyHtml = 1
$MailMessage.Body = $MailBody
$SmtpClient.Send($MailMessage)

c:\scripts\lockedout.html
ASKER CERTIFIED SOLUTION
Avatar of Shaun Vermaak
Shaun Vermaak
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Best solution