Advice on a script

Welcome Forums General PowerShell Q&A Advice on a script

Viewing 9 reply threads
  • Author
    Posts
    • #252311
      Participant
      Topics: 8
      Replies: 12
      Points: 168
      Rank: Participant

      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

    • #252314
      Participant
      Topics: 12
      Replies: 38
      Points: 282
      Helping Hand
      Rank: Contributor

      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:

      Hope this can help you.

    • #252332
      Participant
      Topics: 8
      Replies: 12
      Points: 168
      Rank: Participant

      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:

      Hope this can help you.

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

       

      Rich

    • #252350
      Participant
      Topics: 15
      Replies: 1775
      Points: 3,214
      Helping Hand
      Rank: Community Hero

      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.

      An example of processing would look like:

      Output:

    • #252419
      Participant
      Topics: 8
      Replies: 12
      Points: 168
      Rank: Participant

      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.

      An example of processing would look like:

      Output:

      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

    • #252452
      Participant
      Topics: 8
      Replies: 568
      Points: 2,170
      Helping Hand
      Rank: Community Hero

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

    • #252464
      Participant
      Topics: 7
      Replies: 2458
      Points: 6,439
      Helping Hand
      Rank: Community MVP

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

    • #252566
      Participant
      Topics: 8
      Replies: 568
      Points: 2,170
      Helping Hand
      Rank: Community Hero

      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.

    • #252572
      Participant
      Topics: 7
      Replies: 2458
      Points: 6,439
      Helping Hand
      Rank: Community MVP

      … but if it matches the name completely it’s not required.

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

    • #252590
      Participant
      Topics: 15
      Replies: 1775
      Points: 3,214
      Helping Hand
      Rank: Community Hero

      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.

Viewing 9 reply threads
  • You must be logged in to reply to this topic.