Good morning fellow Powershellers! Hope this finds everyone well. I am currently in the process of creating a New-ADUser script and I would like advice on how to accomplish something. I work for a school district and we have about 42 different cost centers each having a number like 9020, 1251, 0451, etc. I know I could have a variable for each one and do a ton of if statements to put the user in the correct OU, but I was wondering if there is another way that I am not aware of? So basically I have 42 different OU’s these new users can go in depending on what cost center they will be working. Could anyone give me another way to accomplish this other than a ton of variables? Thank you in advanced. Hope everyone has a good day!
Just build the path with a variable, no need for switch unless you have multiple costcenters that go into OU’s or soemthing like that.
$costcenter = 1234
$ouPath = 'OU={0},OU=Districts,DC=myschool,DC=edu' -f $costcenter
'Adding user to OU: {0}' -f $ouPath
An example of processing would look like:
$costcenters = 1234,4524,2223,4532
foreach ($costcenter in $costcenters) {
$ouPath = 'OU={0},OU=Districts,DC=myschool,DC=edu' -f $costcenter
'Adding user to OU: {0}' -f $ouPath
}
Output:
Adding user to OU: OU=1234,OU=Districts,DC=myschool,DC=edu
Adding user to OU: OU=4524,OU=Districts,DC=myschool,DC=edu
Adding user to OU: OU=2223,OU=Districts,DC=myschool,DC=edu
Adding user to OU: OU=4532,OU=Districts,DC=myschool,DC=edu
[quote quote=252350]Just build the path with a variable, no need for switch unless you have multiple costcenters that go into OU’s or soemthing like that.
<textarea class="urvanov-syntax-highlighter-plain print-no" style="tab-size: 4; font-size: 14px !important; line-height: 18px !important; z-index: 0; opacity: 0;" readonly="readonly" data-settings="dblclick">Adding user to OU: OU=1234,OU=Districts,DC=myschool,DC=edu
Adding user to OU: OU=4524,OU=Districts,DC=myschool,DC=edu
Adding user to OU: OU=2223,OU=Districts,DC=myschool,DC=edu
Adding user to OU: OU=4532,OU=Districts,DC=myschool,DC=edu</textarea>
1
2
3
4
Adding user to OU:OU=1234,OU=Districts,DC=myschool,DC=edu
Adding user to OU:OU=4524,OU=Districts,DC=myschool,DC=edu
Adding user to OU:OU=2223,OU=Districts,DC=myschool,DC=edu
Adding user to OU:OU=4532,OU=Districts,DC=myschool,DC=edu
[/quote]
Hey Rob thanks again for replying. Unfortunately we have our CostCenter OU's pretty much all over the place in Active Directory. I started messing with the switch statement today and I have it working ok. I just hate messy looking code. Was hoping there was an easier way of doing this that I didn't know of. I greatly appreciate your response though. Stay safe out there!
You know for several things you do, but if it matches the name completely it’s not required. I tested Get-ADGroup, Get-ADComputer, Get-ADOrganizationalUnit. However, if it’s a partial match then you definitely need it.
OU: SomeName
Get-ADOrg -Filter "Name -like 'name'" # won't find it
Get-ADOrg -Filter "Name -like 'somename'" # will find
Get-ADOrg -Filter "Name -like '*name'" # will find
This also assumes there is a 1:1 match for that costcenter search. If there are no matches or multiple matches, you need to have a default OU to place objects.
$costcenter = 1234
$oupath = Get-ADOrganizationalUnit -Filter "name -like '$costcenter'" | Select-Object -ExpandProperty distinguishedname
if (@($oupath).Count -ne 1) {
$ouPath = 'OU=MyDefaultOU,OU=Districts,DC=myschool,DC=edu'
'Search for costcenter {0} returned {1} matches, using default path {3}' -f $costcenter,$oupath,$oupath
}
Add-AdUser ...