Script Repository


Users whose e-mail address has been recently changed

January 11, 2017
2078

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 )
No results found.
Leave a comment