Distribution Group based on Head of Organization

Another Day another PowerShell Spell

Recently, I have been contacted by one of our clients to create a solution that will automate the membership of a distribution group based on organization head.

Any users under hierarchy of Vice president should be member of this group. (so we will need to recursively fetch the direct reports from the head and then go down level till we reach zero)

Download the Script from our git repository:

https://github.com/VikasSukhija/Downloads/blob/master/DynamicDLBasedonHead.ps1

Prerequisites: Active Directory Module

To run the Script, you can run it from PowerShell

.\DynamicDLBasedonHead.ps1 -Head ‘samaccountnameofVPorManager’ -ADgroup ‘ADGroup’ -smtpserver smtpserver -from DoNotReply@labtest.com -erroremail Reportslogs@labtest.com -countofchanges 500

Head à
Samaccountname of the head of the organization (VP or manager for whom hierarchy you want to be member of the group)

ADGroup à
Samaccountname of the group

Smtpserver
à Smtp server

From
à from address from which email will be sent

Erroremail à email address on which log, or error will be sent.

Countofchanges à This has been built for protection so that if you see more changes than this number script stops and does not process anything.

Logs are placed in logs folder and also recycled after 60 days (you can update the variable inside the script if you want more days)

Note: First time add a single member to the group manually and count of changes should be high so that all members can be added.


To schedule a script, you can create a batch file and add it in task scheduler.

Powershell .\DynamicDLBasedonHead.ps1 -Head ‘samaccountnameofVPorManager’ -ADgroup ‘ADGroup1’ -smtpserver smtpserver -from DoNotReply@labtest.com -erroremail Reportslogs@labtest.com -countofchanges 500

Powershell .\DynamicDLBasedonHead.ps1 -Head ‘samaccountnameofVPorManager’ -ADgroup ‘ADGroup2’ -smtpserver smtpserver -from DoNotReply@labtest.com -erroremail Reportslogs@labtest.com -countofchanges 500



I hope this script will help many as this requirement seems to be interesting.

I also want to give credits to layzwinamdin from where I have taken the recursive function. (
Get-ADdirectReports)

https://lazywinadmin.com/2014/10/powershell-who-reports-to-whom-active.html

 

 

Thanks for reading and downloading….

Tech Wizard

https://techwizard.cloud

https://syscloudpro.com/

PowerShell Cheat Book

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s