Welcome › Forums › General PowerShell Q&A › Advice on a script
- This topic has 9 replies, 5 voices, and was last updated 4 months, 3 weeks ago by
Participant.
-
AuthorPosts
-
-
August 25, 2020 at 6:15 am #252311
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
-
August 25, 2020 at 7:02 am #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:
PowerShell1234567891011121314151617181920212223242526272829switch ($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.
-
August 25, 2020 at 8:57 am #252332
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>
1234567891011121314151617181920212223242526272829switch ($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.
Thank you for the reply. I can try the switch statement. Have a great day!
Rich
-
August 25, 2020 at 10:51 am #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.
PowerShell12345$costcenter = 1234$ouPath = 'OU={0},OU=Districts,DC=myschool,DC=edu' -f $costcenter'Adding user to OU: {0}' -f $ouPathAn example of processing would look like:
PowerShell123456$costcenters = 1234,4524,2223,4532foreach ($costcenter in $costcenters) {$ouPath = 'OU={0},OU=Districts,DC=myschool,DC=edu' -f $costcenter'Adding user to OU: {0}' -f $ouPath}Output:
PowerShell1234Adding user to OU: OU=1234,OU=Districts,DC=myschool,DC=eduAdding user to OU: OU=4524,OU=Districts,DC=myschool,DC=eduAdding user to OU: OU=2223,OU=Districts,DC=myschool,DC=eduAdding user to OU: OU=4532,OU=Districts,DC=myschool,DC=edu -
August 25, 2020 at 5:45 pm #252419
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>
12345$costcenter = 1234$ouPath = ‘OU={0},OU=Districts,DC=myschool,DC=edu’ -f $costcenter‘Adding user to OU: {0}’ -f $ouPathAn 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,4532foreach ($costcenter in $costcenters) {
$ouPath = ‘OU={0},OU=Districts,DC=myschool,DC=edu’ -f $costcenter
‘Adding user to OU: {0}’ -f $ouPath
}</textarea>123456$costcenters = 1234,4524,2223,4532foreach ($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>1234Adding user to OU: OU=1234,OU=Districts,DC=myschool,DC=eduAdding user to OU: OU=4524,OU=Districts,DC=myschool,DC=eduAdding user to OU: OU=2223,OU=Districts,DC=myschool,DC=eduAdding user to OU: OU=4532,OU=Districts,DC=myschool,DC=eduHey 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
-
August 26, 2020 at 12:26 am #252452
If you don’t mind querying each time you can just look up the OU.
PowerShell123456$costcenters = 1234,4524,2223,4532$costcenters | foreach {$oupath = Get-ADOrganizationalUnit -Filter "name -like '$_'" | Select-Object -ExpandProperty distinguishedname... add user} -
August 26, 2020 at 2:09 am #252464
Don’t you need a wildcard to use the -like filter?
-
August 26, 2020 at 9:08 am #252566
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.
PowerShell1234567OU: SomeNameGet-ADOrg -Filter "Name -like 'name'" # won't find itGet-ADOrg -Filter "Name -like 'somename'" # will findGet-ADOrg -Filter "Name -like '*name'" # will find -
August 26, 2020 at 9:23 am #252572
… but if it matches the name completely it’s not required.
I haven’t had this particular case in my mind. 😉
-
August 26, 2020 at 10:15 am #252590
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.
PowerShell123456789$costcenter = 1234$oupath = Get-ADOrganizationalUnit -Filter "name -like '$costcenter'" | Select-Object -ExpandProperty distinguishednameif (@($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 ...
-
-
AuthorPosts
- The topic ‘Advice on a script’ is closed to new replies.