Advice on a script

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!

Rich

You might want to look into the Switch statement for this.

You don’t say exactly where the info comes into play, so I’ll just do a quick pseudocode example:

switch ($costcenter) {

9020 {

add-user to OU '9020'

break

}

1251 {

add-user to OU '1251'

break

}

… snip …

default {

Write-Error 'No matching OU found'

break

}

}

Hope this can help you.

[quote quote=252314]You might want to look into the Switch statement for this.

You don’t say exactly where the info comes into play, so I’ll just do a quick pseudocode example:

PowerShell
<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">switch ($costcenter) {

9020 {

add-user to OU '9020'

break

}

1251 {

add-user to OU '1251'

break

}

… snip …

default {

Write-Error 'No matching OU found'

break

}

}</textarea>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
switch ($costcenter) {
9020 {
add-user to OU '9020'
break
}
1251 {
add-user to OU '1251'
break
}
... snip ...
default {
Write-Error 'No matching OU found'
break
}
}
Hope this can help you.

[/quote]
Thank you for the reply. I can try the switch statement. Have a great day!

 

Rich

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.

PowerShell
<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">$costcenter = 1234

$ouPath = ‘OU={0},OU=Districts,DC=myschool,DC=edu’ -f $costcenter

‘Adding user to OU: {0}’ -f $ouPath</textarea>

1
2
3
4
5
$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:
PowerShell
<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">$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
}</textarea>

1
2
3
4
5
6
$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:
PowerShell
<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!

Rich

If you don’t mind querying each time you can just look up the OU.

$costcenters = 1234,4524,2223,4532

$costcenters | foreach {
    $oupath = Get-ADOrganizationalUnit -Filter "name -like '$_'" | Select-Object -ExpandProperty distinguishedname
    ... add user
}

Don’t you need a wildcard to use the -like filter?

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

I haven’t had this particular case in my mind. :wink:

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 ...