Mail notification for bulk accounts expiration

Hello everyone,

I'm looking to set up a monthly email notification system for managers, informing them about users whose accounts are set to expire in the upcoming month.

Currently, I have a process in place for individual email notifications, with one email being sent for each user nearing expiration.

However, I'm seeking guidance on how to streamline this for bulk users, consolidating all relevant information into a single monthly email.


Email Example:
Dear Manager,

In the next moth (February) the following accounts will expire:
User1
User2
User3
...

Any advice on how to efficiently manage this process and send just one comprehensive email per month would be greatly appreciated.

Thank you,

Elena

  • I will assume that you already have code for gathering up the list of expiring users.

    I would take that and store it into a text file - e.g. expiring_users.txt

    Then create two other files (suggested):  Notification_top.txt, Notification_bottom.txt to contain the introductory and closing text respectively for your message

    When you run your task (suggest an Active Roles Automation Workflow), your Script Activity can concatenate the contents of the files something like this:

    $MsgBody = (Get-Content Notification_top.txt) + (Get-Content expiring_users.txt) + (Get-Content Notification_bottom.txt)

    Take a look at "MailKit" on GitHub as a mechanism for sending the message from your Script Activity.  The body of your message will be in the $MsgBody.

    You will have to consider how you you will derive the "To:" field for the message but a simple way would be to obtain the contents of the Manager attribute for one of the users in your list (this will give you a distinguishedname).  You can then use Get-ADUser with that distinguished to get the contents of the mail attribute for the actual Manager.  Supply that as the "To:" field to the message sending mechanism.

    Anyway, that's an approach - put something together and come back here to ask follow-up questions if it doesn't work and/or you get stuck.

  • If you want to do it memory, opposed to outputting to file, you could

    1) Get a list of all users that are going to expired within your timeframe (include the manager attribute), stored in $ExpiringUsers

    2) Get a unique list of manager by $UniqueManagers = $ExpiringUsers | Select-Object Manager -unique

    3) loop through each manager in $UniqueManagers ($UniqueManager in $UniqueManagers)

    a) Get the list of expiring users from the current manager, ie: $CurrentExpiringUsers = $ExpiringUsers | Where-object {$_.Manager -eq $UniqueManager}

    b) Get the current manager details as required for the email

    c) Loop through each $currentExpiringuser, and add the appropriate details to some message body

    c) send email (if manager has email address, otherwise substitute for example the helpdesk email address)

    d) move to the next manager.