Solved

Move Computers from Excel list to OU

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Ever notice how you can't use a new drive in Windows without having Windows assigning a Disk Signature?  Ever have a signature collision problem (especially with Virtual Machines?)  This article is intended to help you understand what's going on and…
When you upgrade from Windows 8 to 8.1 or to Windows 10 or if you are like me you are on the Insider Program you may find yourself with many 450MB recovery partitions.  With a traditional disk that may not be a problem but with relatively smaller SS…
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…
Windows 8 came with a dramatically different user interface known as Metro. Notably missing from that interface was a Start button and Start Menu. Microsoft responded to negative user feedback of the Metro interface, bringing back the Start button a…

757 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