Mail Notification Script for Domain User Account Enabling or Disabling by Admin

Anonymous
2024-11-27T07:02:42+00:00

Hello

When admin Disable or Enable the Domain User Account. Mail Notification should be sent.
Below the script when i tried with Event ID

Define SMTP and email parameters

$SMTPServer = "smtp.yourdomain.com"

$From = "******@yourdomain.com"

$To = "******@yourdomain.com"

Get recent Security events for account enable/disable

$EventIDs = @(4722, 4725)

$Events = Get-WinEvent -FilterHashtable @{

LogName = "Security" 

Id = $EventIDs 

} -MaxEvents 5

foreach ($Event in $Events) {

# Extract event details 

$EventID = $Event.Id 

$Action = if ($EventID -eq 4722) { "Enabled" } else { "Disabled" } 

$Time = $Event.TimeCreated 

$Details = $Event.Properties 

# Output event properties for inspection 

Write-Output $Details 

# Adjust indexes based on inspection 

$TargetAccount = $Details[5].Value    # Affected account (Check this index) 

$Initiator = $Details[1].Value       # Admin who initiated the action (Check this index) 

$TargetOU = $Details[6].Value        # Organizational Unit of the account (Check this index) 

# Email body 

$Body = @" 

AD Account Status Changed: 

Action: $Action 

Account: $TargetAccount 

Changed By: $Initiator 

Target OU: $TargetOU 

Time: $Time 

"@ 

# Send email 

Send-MailMessage -SmtpServer $SMTPServer -From $From -To $To -Subject "AD Account Status Changed" -Body $Body 

}

Result:-
Getting mail notification

Action: Enabled

Account: TLS

Changed By:

Target OU:

Time: 11/27/2024 07:53:00

Not Getting Exact Domain User for
Account: TLS

Changed By:

Target OU:

What is missing?

Windows Server Remote and virtual desktops PowerShell

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question. To protect privacy, user profiles for migrated questions are anonymized.

0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Anonymous
    2024-11-28T00:03:15+00:00

    Hi,

    Have you checked the properties of Event 4722 and 4725 messages? According to the help files below, the TargetUserName is $Details[0], the SubjectUserName is $Details[4] and the OU of the target account cannot be found in these events.

    4722(S) A user account was enabled. - Windows 10 | Microsoft Learn

    4725(S) A user account was disabled. - Windows 10 | Microsoft Learn

    0 comments No comments
  2. Anonymous
    2024-11-28T06:27:24+00:00

    Hi,

    Have you checked the properties of Event 4722 and 4725 messages? According to the help files below, the TargetUserName is $Details[0], the SubjectUserName is $Details[4] and the OU of the target account cannot be found in these events.

    4722(S) A user account was enabled. - Windows 10 | Microsoft Learn

    4725(S) A user account was disabled. - Windows 10 | Microsoft Learn

    I am not getting your point, what changes should be applied in the script. can you update with my script please @lan-Xue

    0 comments No comments
  3. Anonymous
    2024-11-29T02:30:33+00:00

    I mean TargetUserName is the first field of the properties (which should be $Details[0] in your script) and SubjectUserName is the fifth field (which is $Details[4]).

    $TargetAccount = $Details[0].Value # Affected account (Check this index)
    
    $Initiator = $Details[4].Value # Admin who initiated the action (Check this index)
    

    And there is no Organizational Unit of the account in the events. You can get that from the DistinguishedName of the target AD account.

    $TargetOU = (Get-ADUser $TargetAccount).DistinguishedName -replace '^.*?,(?=[A-Z]{2}=)'
    
    0 comments No comments