Move a list of computer to an OU (Full Version)

All Forums >> [Scripting Technologies] >> VB Script



Message


mroberts -> Move a list of computer to an OU (12/29/2004 3:30:24 PM)

I ran a script against all the PC' s in our domain that pinged them and outputted the nulled ones to a text file.

I found a script from technet that will move a single computer from a container to an OU. I would like to move a text file of computers to a different ou and disable them.

Here is the script from technet. How do I modify it to move a text file of computers or all the computers that are null when pinged?

Set objNewOU = GetObject(" LDAP://OU=Finance,DC=fabrikam,DC=com" )
Set objMoveComputer = objNewOU.MoveHere _
    (" LDAP://CN=atl-pro-03,CN=Computers,DC=fabrikam,DC=com" , " CN=atl-pro-03" )
 




rsasse -> RE: Move a list of computer to an OU (12/29/2004 4:05:02 PM)

Try this! Didn' t have time to test it, so I hope for the best :o)

Create a textfile with computernames ( one name per line) and insert the name of the textfile where it says TextfileWithComputernames.txt . Script and textfile must be in the same directory.

File with script code is attached.





mroberts -> RE: Move a list of computer to an OU (12/29/2004 4:14:22 PM)

I can not get to your attachment.




rsasse -> RE: RE: Move a list of computer to an OU (12/29/2004 6:04:15 PM)

I' m on the road for the rest of the day! Will repost again tomorrow when I' m back at the office.

Sorry,




rsasse -> Move a list of computer to an OU (12/30/2004 12:02:34 PM)

I' m having trouble getting it uploaded. When I type it in here it doesn' t show and when I upload the textfile it can' t be accessed. Not sure why this is happening. I can email it to you if you want.






dthomson -> RE: Move a list of computer to an OU (12/30/2004 12:20:42 PM)

I moved the error checking code into the loop to improve the error checking. This script is best run with cscript.exe.

On Error Resume Next

Set objFSO = CreateObject(" Scripting.FileSystemObject" )
Set objFile = objFSO.OpenTextFile(" TextFileWithComputernames.txt" , 1)

Do Until objFile.AtEndOfStream

Computer = objFile.ReadLine

Set objNewOU = GetObject_
(" LDAP://OU=Finance,DC=fabrikam,DC=com)
Err.Clear
Set objMoveComputer = objNewOU.MoveHere_
(" LDAP://CN=" & Computer_
& " ,CN=Computers,DC=Fabrikam,DC=com" , " CN=" & Computer)

If Err.Number <> 0 Then
Wscript.Echo " Error moving " & Computer & " . (" & Err.Number & " )"
Else
WScript.Echo " Successfully moved " & Computer & " ."
End if
Loop
objFile.Close

Wscript.echo " Done"




rsasse -> RE: Move a list of computer to an OU (12/30/2004 3:49:43 PM)

cool!

I guess it was there afterall.
Thanx, Dan for looking over it[:D]




wsoutherland -> RE: Move a list of computer to an OU (1/3/2005 11:01:35 AM)

If you have a list of computers but they' re in different OUs or you don' t know which OU their in, this script reads the computer names from the text file, searches AD for the path and passes that as the " from" variable. You just specify the " to" OU on line 61. Change lines 40, 41 and 61 to fit your environment.

Specifying the PDC Emulator isn' t required, but highly recommended. Especially recommended in medium to large AD environments (5 DCs or more). Sorry, no error control. It either work or it don' t. :)

On error resume next

' Declare Variables
Dim ObjCompPath
Dim ObjCompCN
Dim ObjComputer

' Begin procedure to read from text file
Const ForReading = 1
Set objDictionary = CreateObject(" Scripting.Dictionary" )
Set objFSO = CreateObject(" Scripting.FileSystemObject" )

' Line to feed script a text list of computernames to search Active Directory
CompFile = InputBox(" Enter the path to the text file with computer names:" )


Set objTextFile = objFSO.OpenTextFile(CompFile, ForReading)

i = 0
Do Until objTextFile.AtEndOfStream
strNextLine = objTextFile.Readline
objDictionary.Add i, strNextLine
i = i + 1

Loop

For Each objItem in objDictionary


Set objConnection = CreateObject(" ADODB.Connection" )
objConnection.Open " Provider=ADsDSOObject;"


' Binds the connection to a command

Set objCommand = CreateObject(" ADODB.Command" )
objCommand.ActiveConnection = objConnection

' LDAP filter to search Active Directory for each computer from provided text file

objCommand.CommandText = " <LDAP://PDC_EMULATOR/dc=DOMAINNAME,dc=COM>;(&(objectcategory=computer)(cn=" & objDictionary.Item(objItem) & " ));ADsPath;subtree"

Set objRecordSet = objCommand.Execute
While Not objRecordset.EOF

' If computer found, distinguished path and CN name to another script

strADsPath = objRecordset.Fields(" ADsPath" )
set ObjComputer = GetObject(strADsPath)

ObjCompPath = ObjComputer.DistinguishedName
ObjCompCN = ObjComputer.CN
objRecordset.MoveNext
Wend

objConnection.Close

' Operations to move to new OU

Set objNewOU = GetObject(" LDAP://PDC_EMULATOR/OU=TARGET_OU,DC=DOMAINNAME,DC=COM" )
Set objMoveComputer = objNewOU.MoveHere (" LDAP://PDC_EMULATOR/" & ObjCompPath, " CN=" & ObjCompCN)

Next




jbowen -> RE: RE: Move a list of computer to an OU (1/5/2005 1:48:11 PM)

quote:



objCommand.CommandText = " <LDAP://PDC_EMULATOR/dc=DOMAINNAME,dc=COM>;(& objectcategory=computer)(cn=" & objDictionary.Item(objItem) & " ));ADsPath;subtree"

Set objNewOU = GetObject(" LDAP://PDC_EMULATOR/OU=TARGET_OU,DC=DOMAINNAME,DC=COM" )

Set objMoveComputer = objNewOU.MoveHere (" LDAP://PDC_EMULATOR/" & ObjCompPath, " CN=" & ObjCompCN)



Not sure what you are referencing here but I had to delete everywhere you mention PDC_EMULATOR/. Chaning the reset of the line to match my given situation was a given [:D]

Thanks for the script. You just saved me a TON of manual labor.




jbowen -> RE: Move a list of computer to an OU (1/10/2005 11:29:58 AM)

edit - 01/10/05
*** I figured it out. The txt file I was using did not have the CN correct. I was attempting to use the login name. Once I corrected that all is good.

I changed this to allow me to move users. However something really wierd is happening. The script worked flawlessly two days ago. Now it doesn' t work at all.

Any help?


quote:


On error resume next

' Declare Variables
Dim ObjUserPath
Dim ObjUserCN
Dim ObjUser


' Begin procedure to read from text file
Const ForReading = 1
Set objDictionary = CreateObject(" Scripting.Dictionary" )
Set objFSO = CreateObject(" Scripting.FileSystemObject" )

' Line to feed script a text list of Usernames to search Active Directory
UserFile = InputBox(" Enter the path & file name of the text file with User names:" )

Set objTextFile = objFSO.OpenTextFile(UserFile, ForReading)

i = 0
Do Until objTextFile.AtEndOfStream
strNextLine = objTextFile.Readline
objDictionary.Add i, strNextLine
i = i + 1

Loop

For Each objItem in objDictionary


Set objConnection = CreateObject(" ADODB.Connection" )
objConnection.Open " Provider=ADsDSOObject;"


' Binds the connection to a command

Set objCommand = CreateObject(" ADODB.Command" )
objCommand.ActiveConnection = objConnection

' LDAP filter to search Active Directory for each User from provided text file

objCommand.CommandText = " <LDAP://dc=corp,dc=jhhs,dc=org>;(&(objectcategory=User)(cn=" & objDictionary.Item(objItem) & " ));ADsPath;subtree"
' objCommand.CommandText = " <LDAP://PDC_EMULATOR/dc=DOMAINNAME,dc=COM>;(&(objectcategory=computer)(cn=" & objDictionary.Item(objItem) & " ));ADsPath;subtree"

Set objRecordSet = objCommand.Execute
While Not objRecordset.EOF

' If User found, distinguished path and CN name to another script

strADsPath = objRecordset.Fields(" ADsPath" )
Set ObjUser = GetObject(strADsPath)

ObjUserPath = ObjUser.DistinguishedName
ObjUserCN = ObjUser.CN
objRecordset.MoveNext
Wend

objConnection.Close

' Operations to move to new OU

Set objNewOU = GetObject(" LDAP://10001 - Nursing Administration,DC=corp,DC=jhhs,DC=org" )
Set objMoveUser = objNewOU.MoveHere (" LDAP://" & ObjUserPath, " CN=" & ObjUserCN)
' Set objNewOU = GetObject(" LDAP://PDC_EMULATOR/OU=TARGET_OU,DC=DOMAINNAME,DC=COM" )
' Set objMoveComputer = objNewOU.MoveHere (" LDAP://PDC_EMULATOR/" & ObjCompPath, " CN=" & ObjCompCN)

Next
[&:][&:][8D]




mroberts -> RE: Move a list of computer to an OU (2/4/2005 3:20:32 PM)

I' m not having any luck with this. When reume error is off I the script pops ups and says error moving PC name.

When I enable resume error I get an error " there is no such object on the server on line 13.

Here is the code I' m using, any help please

' On Error Resume Next 

Set objFSO = CreateObject(" Scripting.FileSystemObject" ) 
Set objFile = objFSO.OpenTextFile(" c:\test.txt" , 1) 

Do Until objFile.AtEndOfStream 

Computer = objFile.ReadLine 

Set objNewOU = GetObject _
(" LDAP://OU=Inactive Computer Accounts,DC=test,DC=net" ) '  OU to move PC to
Err.Clear 
Set objMoveComputer = objNewOU.MoveHere _ 
(" LDAP://CN= & Computer,OU=BPS Computers,DC=test,DC=net" , " CN= & Computer" ) '  Target OU

If Err.Number <> 0 Then 
Wscript.Echo " Error moving "  & Computer & " . ("  & Err.Number & " )"  
Else 
WScript.Echo " Successfully moved "  & Computer & " ."  
End if 
Loop 
objFile.Close 

Wscript.echo " Done"  




mcarriere893 -> RE: Move a list of computer to an OU (2/4/2005 4:37:55 PM)

Wrong- sorry




mroberts -> RE: Move a list of computer to an OU (2/4/2005 4:45:29 PM)

?




dthomson -> RE: RE: Move a list of computer to an OU (2/6/2005 5:50:51 PM)

I didn' t test this, but I see some " characters missing

Replace this:
quote:

Set objMoveComputer = objNewOU.MoveHere _
(" LDAP://CN= & Computer,OU=BPS Computers,DC=test,DC=net" , " CN= & Computer" ) ' Target OU


with
quote:

Set objMoveComputer = objNewOU.MoveHere _
(" LDAP://CN=" & Computer & " ,OU=BPS Computers,DC=test,DC=net" ,CN=" & Computer) ' Target OU

HTH




mroberts -> RE: Move a list of computer to an OU (2/7/2005 10:32:33 AM)

Did that now I get a unterminated string constant on that line.




dthomson -> RE: RE: Move a list of computer to an OU (2/7/2005 10:48:01 AM)

Oops, there' s an extra " in there. Try this one.

Set objMoveComputer = objNewOU.MoveHere _
(" LDAP://CN=" & Computer & " ,OU=BPS Computers,DC=test,DC=net,CN=" & Computer) ' Target OU




mroberts -> RE: Move a list of computer to an OU (2/7/2005 12:00:06 PM)

Error moving PCname (450)




wsoutherland -> RE: RE: RE: Move a list of computer to an OU (2/8/2005 6:12:08 PM)

Everywhere you see PDC_EMULATOR you would replace with the netbios name of the domain controller in active directory that holds the PDC Emulator role. You could specify any DC you want.

I recommend specifying a DC because if you don' t, every time you run the script, it could connect to a different domain controller and move the objects on that domain controller. Worse, each time it makes a separate LDAP query to Active Directory for the location of the computer account, it could be making each query on a different, random DC. The changes, of course, replicate to all the other DCs, but how long does/could that take?

Specifying a specific DC keeps the scripts connection constant. The DC with the role of PDC Emulator holds priority over all other domain controllers in the domain, even in Native or 2003 mode. Replication of changes is also usually faster from that server to the rest of them.




wsoutherland -> RE: RE: Move a list of computer to an OU (2/8/2005 6:23:07 PM)

User accounts are a little different that computer accounts I think for moving. Please try below and let us know:

On error resume next

' Declare Variables
Dim ObjUserPath
Dim ObjUserCN
Dim ObjUser


' Begin procedure to read from text file
Const ForReading = 1
Set objDictionary = CreateObject(" Scripting.Dictionary" )
Set objFSO = CreateObject(" Scripting.FileSystemObject" )

' Line to feed script a text list of Usernames to search Active Directory
UserFile = InputBox(" Enter the path & file name of the text file with User names:" )

Set objTextFile = objFSO.OpenTextFile(UserFile, ForReading)

i = 0
Do Until objTextFile.AtEndOfStream
strNextLine = objTextFile.Readline
objDictionary.Add i, strNextLine
i = i + 1

Loop

For Each objItem in objDictionary


Set objConnection = CreateObject(" ADODB.Connection" )
objConnection.Open " Provider=ADsDSOObject;"


' Binds the connection to a command

Set objCommand = CreateObject(" ADODB.Command" )
objCommand.ActiveConnection = objConnection

' LDAP filter to search Active Directory for each User from provided text file

objCommand.CommandText = " <LDAP://dc=corp,dc=jhhs,dc=org>;(&(objectcategory=User)(cn=" & objDictionary.Item(objItem) & " ));ADsPath;subtree"
' objCommand.CommandText = " <LDAP://PDC_EMULATOR/dc=DOMAINNAME,dc=COM>;(&(objectcategory=computer)(cn=" & objDictionary.Item(objItem) & " ));ADsPath;subtree"

Set objRecordSet = objCommand.Execute
While Not objRecordset.EOF

' If User found, distinguished path and CN name to another script

strADsPath = objRecordset.Fields(" ADsPath" )
Set ObjUser = GetObject(strADsPath)

ObjUserPath = ObjUser.DistinguishedName
ObjUserCN = ObjUser.CN
objRecordset.MoveNext
Wend

objConnection.Close

' Operations to move to new OU

Set objNewOU = GetObject(" LDAP://10001 - Nursing Administration,DC=corp,DC=jhhs,DC=org" )
objNewOU.MoveHere (" LDAP://" & ObjUserPath, vbNullString)
' Set objNewOU = GetObject(" LDAP://PDC_EMULATOR/OU=TARGET_OU,DC=DOMAINNAME,DC=COM" )
' Set objMoveComputer = objNewOU.MoveHere (" LDAP://PDC_EMULATOR/" & ObjCompPath, " CN=" & ObjCompCN)

Next




mroberts -> RE: Move a list of computer to an OU (2/11/2005 8:56:21 AM)

Still not working for me I get error moveing PCname (450)

Here is the complete code I' m using can anyone help?

On Error Resume Next 

Set objFSO = CreateObject(" Scripting.FileSystemObject" ) 
Set objFile = objFSO.OpenTextFile(" c:\test.txt" , 1) 

Do Until objFile.AtEndOfStream 

pc = objFile.ReadLine 

Set objNewOU = GetObject _
(" LDAP://CN=Computers,DC=test,DC=net" ) '  OU to move PC to
Err.Clear 
Set objMoveComputer = objNewOU.MoveHere _ 
(" LDAP://CN="  & pc & " ,OU=test,DC=test,DC=net,CN="  & pc) '  Target OU

If Err.Number <> 0 Then 
Wscript.Echo " Error moving "  & pc & " . ("  & Err.Number & " )"  
Else 
WScript.Echo " Successfully moved "  & pc & " ."  
End if 
Loop 
objFile.Close 

Wscript.echo " Done"  




dthomson -> RE: RE: Move a list of computer to an OU (2/11/2005 10:42:08 AM)

I have to appologize Matt, I steered you wrong when I removed some of the quotes in your original code. [:(]

I was able to successfully run this code on a Windows XP client connecting to Windows Server 2003.

On Error Resume Next
Set objFSO = CreateObject(" Scripting.FileSystemObject" )
Set objFile = objFSO.OpenTextFile(" c:\test.txt" , 1)
Do Until objFile.AtEndOfStream
pc = objFile.ReadLine
Set objNewOU = GetObject _
(" LDAP://OU=TopOU2,DC=Domain,DC=local" ) ' OU to move PC to

Err.Clear

Set objMoveComputer = objNewOU.MoveHere _
(" LDAP://CN=" & pc & " ,OU=SubOU1,OU=TopOU1,DC=Domain,DC=local" ," CN=" & pc) ' Target OU

If Err.Number <> 0 Then
Wscript.Echo " Error moving " & pc & " . (" & Err.Number & " )"
Else
WScript.Echo " Successfully moved " & pc & " ."
End if
Loop
objFile.Close
Wscript.echo " Done"




emacintosh -> RE: Move a list of computer to an OU (2/11/2005 10:46:22 AM)

I think you' re missing a couple of quotes

Set objMoveComputer = objNewOU.MoveHere _
(" LDAP://CN=" & pc & " ,OU=test,DC=test,DC=net" ," CN=" & pc) ' Target OU


Dan beat me to it [:)]




mroberts -> RE: Move a list of computer to an OU (2/15/2005 1:49:57 PM)

Still getting error " moving Pcname"

I know its reading though the text file cause it lists each PC it cannot move.

If I use this code for a single PC it works fine. I tried modified this to move a text file of PC' s but still get same error.

Set objOU = GetObject(" LDAP://OU=Finance,DC=fabrikam,DC=com" )
intReturn = objOU.MoveHere _
    (" LDAP://CN=atl-ws-01,OU=Finance,DC=fabrikam,DC=com" , “cn=finance-ws-01”)







mroberts -> RE: Move a list of computer to an OU (2/15/2005 2:34:30 PM)

Opps its actually working its just popping up with an error.

Thanks all your help is appreciated greatly




SirTom -> RE: Move a list of computer to an OU (3/12/2008 3:10:35 PM)

Just an FYI.
This error is cause by trying to get a return value from MoveHere function.
intReturn = objOU.MoveHere (" LDAP://CN=atl-ws-01,OU=Finance,DC=fabrikam,DC=com" , “cn=finance-ws-01”)

To check for Errors use something like this:

On Error Resume Next
Set objOU = GetObject("
LDAP://OU=Finance,DC=fabrikam,DC=com" )
objOU.MoveHere ("
LDAP://CN=atl-ws-01,OU=Finance,DC=fabrikam,DC=com" , vbNullString )
If err.Number <> 0 then
  Msgbox("Your moved failed!" & chr(10) & Err.Number & chr(10) & Err.Description & chr(10) & err.Source )
Else
  Msgbox("Your Moved was Successfully!")
End If













scrisp -> RE: Move a list of computer to an OU (5/12/2008 5:22:28 AM)

Ok - I've read your posts - and I'm very impressed - (I am a real newbie with VBS)

What I need is very similar but not quite. I have 200 PC accounts to move but my ou structure is more complex in ad

Winchester
  - Workstations
        - Standard Workstations (PCs are here)
London
- Workstations
        - Standard Workstations (PCs need to be here)

Domain = PCNAME.EEC.OEL.COM

I have created a txt file of all my PCs to move - What script do I need . Please help.

Steve




kdsrazor -> RE: Move a list of computer to an OU (5/12/2008 5:35:40 PM)

For those of you having trouble with the scripting, especially those with no scripting experience, you might try a 3rd party solution like DSRAZOR for Windows. It doesn't require any scripting knoweldge and is fully supported by a company with a support staff of engineers to assist you when you need help.

With DSRAZOR you can select computer or user accounts from all over your Domain, then you can press a button to "do" something to them.  For example, you could disable them all, migrate their home directories, and then move them to another OU, all from the same screen.

Check it out at www.visualclick.com/?source=FORitforum





scrisp -> RE: Move a list of computer to an OU (5/13/2008 4:30:46 AM)

Thanks Ken, I've had a look at this app-  seems quite good, the demo dosn't see my AD structure very well, However I am still really interested in going the VB script route as I'd like to understand scripting better.

Steve




kdsrazor -> RE: Move a list of computer to an OU (5/19/2008 5:51:25 PM)

Sure scrisp,

The trial version you downloaded only shows 2-3 OUs and is limited.  In the future you can always send me a PM or go to the website for a demonstration.
Good luck with the scripting.




Page: [1]

Valid CSS!




Forum Software © ASPPlayground.NET Advanced Edition 2.4.5 ANSI
0.4541016