Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is it possible to create a "universal" script that checks the first three alphabets of a hostname and moves the host into the appropriate OU?

E.g. A hostname that begins with TKYxxxxx is a host in Tokyo and when the script detects TKY, it would move this host to the Tokyo site OU.

Would it be possible/recommended to take it a step further and launch this script as part of a login script which checks AD if the hostname exists in AD, and if not, check the hostname and move it to the appropriate site OU. If no matches, leave it in the Computers OU.

Thanks in advance.

share|improve this question
are these devices located in a centralized ou but just not the correct one, something like the computers ou or say a staging ou. Or are they all over the place literally and figuratively? – tony roth Aug 10 '10 at 21:43

The following will do the job but be careful and test 1st to understand how it works!

dsquery computer -name tky* | dsmove -newparent "ou=tokyo,dc=x,dc=com"

share|improve this answer

By "universal", I would say that if your convention is well defined, and know the appropriate LDAP paths, then It is very possible to do this, but you are really going to want to write this script yourself (or have somebody do it for you.) All that you need to do is iterate over your computer objects (usually a "for each" or a "do while" loop), and either do an "if...elseif...else" or a case (or switch) construct to do the magic.

Tip: I usually reserve the "else" (or the default clause of a case statement) as a "WTF" catchall. This is for machines that don't meet the naming convention and alert you. Either print the statement out to the console, write to a logfile (always good practice to log everything), or send an email of all WTF-s.

Tip: When looking just for Windows Servers, I parse the Object.operatingSystem property for the pattern "Windows*Server*". This pattern matches the text of every Windows Server version.

I know how to do this best in VBScript and Perl, but it's very possible in Powershell, JavaScript, Python. Anything that can hook into AD/LDAP, really...

Check out the Microsoft Technet Script Center Repository. You may find something that does close to what you need.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.