Solved

Scripting help, moving users to OU powershell

Posted on 2014-11-03
7
94 Views
Last Modified: 2015-04-15
Hey guys, i have a command. This command will be the foundation of a script, and here is where i need help.
The command is

Get-ADUser -filter {(title -like "Teacher A") -and (company -eq "School Site A")} | Move-ADObject -TargetPath 'DC=Domain, DC=Local'

I hvae to do this for about 50 teachers. I could copy and manually type this command over and over to account for each individual teacher, but is there an easier way to achieve that?
0
Comment
Question by:mrbayIt
  • 4
  • 3
7 Comments
 
LVL 39

Accepted Solution

by:
footech earned 500 total points
ID: 40421086
Modify your filter for the Get-ADUser command so that it returns what you want.  When you use the -like operator you can use the wildcard "*".  So it all depends on if you can create something that will match all your desired accounts (and no others), and that depends on any similarity between accounts for a specific attribute.  Perhaps the following would work.
Get-ADUser -filter {(title -like "Teacher *") -and (company -eq "School Site A")} | Move-ADObject -TargetPath 'DC=Domain, DC=Local'

Open in new window

0
 

Author Comment

by:mrbayIt
ID: 40421814
sounds like a good start, but how do i identify the teachers for the wildcard to work? (i am new to scripting)
0
 
LVL 39

Expert Comment

by:footech
ID: 40422067
That's kind of up to you.  You know how the * wildcard works, right?
Is the title for all teachers similar?
If you're familiar with the use of wildcards at a command prompt, it's pretty much the same concept.  When you're dealing with files, you're only matching against the name.  So you have a command like
dir *.log
dir read*.txt

When dealing with AD objects, you have a lot more possible properties to match against if you so desire.  If we just focus on using the "title" attribute that's fine.  I can't tell you what you have in the title attribute for each user though.  You have to find a word (or part of a word) that's common across all the accounts you want.
0
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 

Author Comment

by:mrbayIt
ID: 40422131
Thanks footech, im new to scripting so no i dont fully understand wildcards.

more specifically, there is no common title, the job title field will have each teachers name inside of it. Here is what i mean

Get-ADUser -filter {(title -like "TeacherNameC") -and (company -eq "School Site A")} | Move-ADObject -TargetPath 'OU=TeacherNameC,DC=Domain, DC=Local'
Get-ADUser -filter {(title -like "TeacherNAMEA") -and (company -eq "School Site A")} | Move-ADObject -TargetPath 'OU=TeacherNameA,DC=Domain, DC=Local'
Get-ADUser -filter {(title -like "TeacherNameB") -and (company -eq "School Site A")} | Move-ADObject -TargetPath 'OU=TeacherNameB,DC=Domain, DC=Local'

is there an easier way to do this, instead of having to write out each teachers individual name mapping it to there own OU
0
 
LVL 39

Expert Comment

by:footech
ID: 40422198
A * means to match zero or more characters, no matter what they are.

What's the point of the title if all it contains is the teacher's name?  There's little more I can suggest here.  You need to find an attribute that each teacher has in common.  If there isn't one, well...  Accounts can be grouped together for different purposes, often by placing them within the same OU, or by making them members of security groups, etc.  If you need to manage accounts according to certain criteria, then you need to design your AD around that criteria.

I don't think I would ever support the idea of each account having its own OU.  I can't think of any logical reason to do so.
0
 

Author Comment

by:mrbayIt
ID: 40422242
Just haven't gone into detail, long story short i used TeacherNameA just for example, this rule really is meant to correspond to students. Thousands of students.

Every Student has there teacher listed under "Job Title" in AD. The script will need to place them in the teachers OU. Ive tested it already, which is why i referenced the original command its just that i am trying to automate this in a more efficient manner rather than writing each teachers name over and over.

Does that make sense now?
0
 
LVL 39

Expert Comment

by:footech
ID: 40422359
That makes a lot more sense.
To do what you're asking requires a list of all the teacher names.  Hopefully the names in the title attribute match exactly the name of the OU.  This list of names can either be obtained from a text file, hard-coded into the script, or perhaps pulled from existing OU structure.  Here's an example of what might work.
$names = "teacher1","teacher2"
foreach ($name in $names)
{
    Get-ADUser -Filter {(Title -eq $name) -and (company -eq "School Site A")} | Move-ADObject -TargetPath "OU=$name,DC=Domain,DC=Local" -WhatIf
}

Open in new window

0

Featured Post

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Join & Write a Comment

How to sign a powershell script so you can prevent tampering, and only allow users to run authorised Powershell scripts
Set OWA language and time zone in Exchange for individuals, all users or per database.
This tutorial will walk an individual through the steps necessary to join and promote the first Windows Server 2012 domain controller into an Active Directory environment running on Windows Server 2008. Determine the location of the FSMO roles by lo…
This tutorial will walk an individual through the process of configuring their Windows Server 2012 domain controller to synchronize its time with a trusted, external resource. Use Google, Bing, or other preferred search engine to locate trusted NTP …

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now