We use cookies to improve your experience.
By your continued use of this site you accept such use.
For more details please see our privacy policy and cookies policy.

Script repository

Users whose e-mail address has been recently changed

February 18, 2021 Views: 4819

The script creates and emails a CSV report containing users whose e-mail address has been recently changed. To track changes in e-mail addresses, the script saves the current e-mail address in Adaxes custom attribute CustomAttributeText1. On each run, the script compares a user's current e-mail address to the address saved in the custom attribute.

To generate such reports on a regular basis, you need to create a scheduled task configured for the Domain-DNS object type. To add the script to your task, use the Run a program or PowerShell script action.

Note: The script uses cmdlets from Adaxes PowerShell module for Active Directory. To run the script, you need to install the PowerShell Module for Active Directory component of Adaxes.

Parameters:

  • $to - Specifies email addresses of the recipient(s) of the report.
  • $from - Specifies the sender.
  • $smtpServer - Specifies the SMTP server that will be used to send the report.
  • $messageSubject - Specifies the email message subject.
  • $messageBody - Specifies the email message body.
  • $csvFilePath - Specifies the path for the generated CSV file.
  • $deleteCsvWhenSent - Specifies whether to delete the CSV file after it has been sent.
    Edit Remove
    PowerShell
    Import-Module Adaxes
    
    # Email message setings
    $to = "recipient@domain.com" # TODO: modify me
    $from = "noreply@domain.com" # TODO: modify me
    $smtpServer = "mail.domain.com" # TODO: modify me
    $messageSubject = "My Subject" # TODO: modify me
    $messageBody = "Message Text" # TODO: modify me
    
    # CSV file settings
    $csvFilePath = "\\server\share\report.csv" # TODO: modify me
    $deleteCsvWhenSent = $False # Set to $True to delete the CSV file after it is sent
    
    # Find all user accounts
    $users = Get-AdmUser -Filter * -AdaxesService localhost `
        -Properties employeeID, displayName, mail, manager, l, st, whenCreated, whenChanged
    
    $report = @()
    foreach ($user in $users)
    {
        $userMail = $user.mail
        if ($userMail -eq $NULL)
        {
            $userMail = ""
        }
        # Get previously saved email address
        $userObject = $Context.BindToObjectByDN($user.distinguishedName)
        try
        {
            $savedEmailAddress = $userObject.Get("adm-CustomAttributeText1")
        }
        catch
        {
            $savedEmailAddress = $NULL
        }
        if (!$savedEmailAddress)
        {
            # The script is executed for the first time on the user
            $userObject.Put("adm-CustomAttributeText1", $userMail)
            $userObject.SetInfo()
            continue
        }
        if($userMail -ieq $savedEmailAddress)
        {
            # The user's email has not changed since the previous run
            continue
        }
        
        # The user's email has changed since the previous run
        # Update the custom attribute
        $userObject.Put("adm-CustomAttributeText1", $userMail)
        $userObject.SetInfo()
        
        # Add the user to the CSV file
        $newPSObject = New-Object PSObject
        $newPSObject | Add-Member -Name employeeID -Value $user.employeeID -MemberType NoteProperty
        $newPSObject | Add-Member -Name displayName -Value $user.displayName -MemberType NoteProperty
        $newPSObject | Add-Member -Name samaccountname -Value $user.samaccountname -MemberType NoteProperty
        $newPSObject | Add-Member -Name mail -Value $user.mail -MemberType NoteProperty
        $newPSObject | Add-Member -Name manager -Value $user.manager -MemberType NoteProperty
        $newPSObject | Add-Member -Name l -Value $user.l -MemberType NoteProperty
        $newPSObject | Add-Member -Name st -Value $user.st -MemberType NoteProperty
        $newPSObject | Add-Member -Name whenCreated -Value $user.whenCreated -MemberType NoteProperty
        $newPSObject | Add-Member -Name whenChanged -Value $user.whenChanged -MemberType NoteProperty
        $report += $newPSObject
    }
    if ($report.Length -eq 0)
    {
        return # No changes
    }
    
    # Export the report to a CSV file
    $report | Export-Csv -Path $csvFilePath -NoTypeInformation
    
    # Send report
    Send-MailMessage -To $to -from $from -SmtpServer $smtpServer -Attachments $csvFilePath -Subject $messageSubject -Body $messageBody
    
    # Delete the CSV file, if necessary
    if ($deleteCsvWhenSent)
    {
        Remove-Item -Path $csvFilePath -Force
    }
Comments 0
Leave a comment
Loading...

Got questions?

Support Questions & Answers