I’m working on a module for creating users in our AD. We currently have a batch script doing this work in one big file, but I want to break it down into separate functions and package it as a module.
Here is the structure of the module as it looks today:
I only export the functions from the Public folder and anything in Private is called by New-MGADUser.ps1.
Convert-SpecialCharacters.ps1 contains a function of the same name and takes a string, converts it to lower case and replaces characters such as ä,ö and å with a, o and a respectively and returns the converted string.
New-MGStudentAccountName.ps1 is still a work in progress but basically it will take the converted names, grab the first two characters from first name and and the first four from last name and finally prefix the current year.
New-MGADUser.ps1 should be accessible from the command line and take the first and last name of a new student and from that call the functions in Private in order to create a SAM account name matching our template and finally passing everything to New-ADUser from the ActiveDirectory module.
I actually have two questions but my primary concern is of the flow(?) of the thing. Most recommendations go towards splitting up tasks in separate smaller functions for increased readability and simplicity. I get that, I’m just not sure how to write the calling function to get the right result.
Let me try to make a rough skelefon of what I imagine:
New student: Göran Olof Västergren
$firstName = “Göran Olof” passed to Convert-SpecialCharacters.ps1 and returned as $firstName = “goranolof”
$lastName = “Västergren” passed to Convert-SpecialCharacters.ps1 and returned as $lastName = “vastergren”
$firstName and $lastName passed to New-MGStudentAccountName.ps1 and returned as $SAMAccountName: “19govast”
In the end I want to be able to create a new user with a unique SAM account name, place the user in the right OU and groups. But first I need to get a clearer image of how to pass the different variables through the different functions to acheive the first steps.
How do I pass the name through the functions in Private to get my desired outcome?
Should each function be a separate file in Private?