Solved

Move Computers from Excel list to OU

Posted on 2014-03-18
2
319 Views
Last Modified: 2014-03-19
I need your expertise in scripting. I have a EXCEL file that would include about 200 computers and new OU location where I would like the computers to be moved to.


HOSTNAME      NEWLOCATION
GHSSOC01      OU=SSO-Type1,OU=MHG,OU=Workstations,DC=TESTDOMAIN,DC=local
GHSSOC02      OU=SSO-Type1,OU=MHG,OU=Workstations,DC=TESTDOMAIN,DC=local
GHSSOC03      OU=SSO-Type2,OU=MHG,OU=Workstations,DC=TESTDOMAIN,DC=local

I would like to run a script the morning of implemintation that would move the above computers to their perspective OU.
0
Comment
Question by:epicazo
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 19

Accepted Solution

by:
helpfinder earned 500 total points
ID: 39937575
check thisVisual basic script:
on error resume next 
 
CONST ForReading = 1 
Const ForAppending = 8 
 
Set oFS = CreateObject("Scripting.FileSystemObject") 
Set oTS = oFS.OpenTextFile("oldcomputers.txt",ForReading) 
set oLS = oFS.OpenTextFile("movecomputerlog.txt",ForAppending,True) 
Set objRootDSE = GetObject("LDAP://RootDSE") 
strDNSDomain = objRootDSE.Get("defaultNamingContext") 
Set objCommand = CreateObject("ADODB.Command") 
Set objConnection = CreateObject("ADODB.Connection") 
objConnection.Provider = "ADsDSOObject" 
objConnection.Open "Active Directory Provider" 
objCommand.ActiveConnection = objConnection 
 
'Modify the following line to desired OU 
Set objNewOU = GetObject("LDAP://OU=Disabled Computers,dc=test,dc=local") 
Do Until oTS.AtEndOfStream 
        strComputer = oTS.ReadLine 
        strQuery = _ 
          "<LDAP://" & strDNSDomain & ">;(&(objectCategory=computer)(cn=" & strComputer & "));adspath,cn;subtree" 
        objCommand.CommandText = strQuery 
        Set RS = objCommand.Execute 
        While not RS.EOF 
                strPath = RS.Fields("adspath") 
                strName = "CN=" & RS.Fields("cn") 
                oLS.Writeline(strPath) 
                Set objMoveComputer = objNewOU.MoveHere(strPath,strName) 
                RS.MoveNext 
        Wend 
Loop 

Open in new window


Source
0
 

Author Closing Comment

by:epicazo
ID: 39939993
Thanks a lot.. works like a charm!
0

Featured Post

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is a little timesaver I have been using for setting up Microsoft Small Business Server (SBS) in the simplest possible way. It may not be appropriate for every customer. However, when you get a situation where the person who owns the server is i…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Windows 8 comes with a dramatically different user interface known as Metro. Notably missing from the new interface is a Start button and Start Menu. Many users do not like it, much preferring the interface of earlier versions — Windows 7, Windows X…
In this video, we discuss why the need for additional vertical screen space has become more important in recent years, namely, due to the transition in the marketplace of 4x3 computer screens to 16x9 and 16x10 screens (so-called widescreen format). …
Suggested Courses

636 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