The task is to create a script. which will add a number of users to a certain Exchange role. To keep other people from change the script, we will prepare a config file and a CSV. the config file will provide important general information:
role name
location of the log file
location of the csv file
delimiter of the csv file
mail address field title of the csv file
givenname field of the csv file
surname field of the csv file
The CSV will be really simple, as long as you don’t need to track more information then necessary. We would only need the mail address, but sometimes a users name might help.
After creating the config and preparing the CSV, we can finally take a look at Powershell. First off: Load the config file!
Now we are connecting to different cloud services, in this case Exchange Online and Azure AD:
Here’s the part where we handle multi things, that are just busy work:
get the date to create a unique log file name
import the user list
create a header for our log file
Of course we could have created a own csv file instead of using transcript, but the beauty of transcript is, that it will log every error.
Now here’s the heart of the script:
use a foreach to iterate through every user in the csv
try to get the user via mail adress from AzureAD
this is important, because we have to check if the user existsAdd the user to the Exchange Online role
Just some housecleaning stuff:
stop transcript
disconnect from the cloud services
You can find the final code on Github or down below: