Solved

Move Computers from Excel list to OU

Posted on 2014-03-18
2
310 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
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Have you considered what group policies are backwards and forwards compatible? Windows Active Directory servers and clients use group policy templates to deploy sets of policies within your domain. But, there is a catch to deploying policies. The…
Many admins will agree: WSUS is is a nice invention but using it on the client side when updating a newly installed computer is still time consuming as you have to do several reboots and furthermore, the procedure of installing updates, rebooting an…
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…
With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…

948 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

21 Experts available now in Live!

Get 1:1 Help Now