Link to home
Start Free TrialLog in
Avatar of bsharath
bsharathFlag for India

asked on

Sort the machine names in a file to the correct OU's mentioned in the script

Hi,

I have a list of machine names (Computer names) in a file.Need a script which can find these machine names from the file and sent to the correct OU as mentioned in the script.
I have Ou's created according to locations.
Regards
Sharath
Avatar of merowinger
merowinger
Flag of Germany image

OK i think your file looks like this:

Name   OU
pc1      SalesPCs
pc2      SalesPCs
pc3      MarketingPCs
...




I think this the should look like this....

On Error Resume Next
dim objFSO, objFile
dim currentPC, CurrentOU, temp, SpacePos

set objFSO = CreateObject("Scripting.FileSystemObject")
set objFile = objFSO.OpenTextFile("C:\YourPCs.txt)

Const ADS_SCOPE_SUBTREE = 2
Set objConnection = CreateObject("ADODB.Connection")
Set objCommand =   CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
Set objCommand.ActiveConnection = objConnection

objCommand.Properties("Page Size") = 1000
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE


do while not objFile.AtEndOfStream
      temp = objFile.ReadLine()
      SpacePos = InStr(temp, " ")
      currentPC = mid(temp,0,SpacePos)
      CurrentOU = mid(temp,SpacePos,50)      'Your OU Informations in Your File must look like this "LDAP://ou=Finance,dc=fabrikam,dc=com"


      objOU = GetObject(CurrentOU)


      objCommand.CommandText = "SELECT ADsPath FROM 'LDAP://dc=YourDomain,dc=com' WHERE objectCategory='computer' AND Name='" &CurrentPC &"'"
      Set objRecordSet = objCommand.Execute

      objRecordSet.MoveFirst

      Do Until objRecordSet.EOF
                strADsPath = objRecordSet.Fields("ADsPath").Value
               objOU.MoveHere strADsPath, vbNullString
                objRecordSet.MoveNext
      Loop
loop
Avatar of bsharath

ASKER

I only have the pc names in the txt file.

pc1
pc2
all these pc's can be in different Ou's.So i cannot specify the Ou paths.

But i know which pc's has to be in which Ou.
Yes the Source is not important..the script seaches the whole ad where the pc is located but
important is the path where the should moved!

So when u know it, it would be good that u write down which pc has to be moved in which ou
Which are the paths i need to make changes according to my domain
i meant your file should look like this

pc1 LDAP://ou=Sales,dc=YourDomain,dc=com
pc2 LDAP://ou=YourDomain,dc=YourDomain,dc=com
pc3 LDAP://ou=Marketing,dc=YourDomain,dc=com
pc4 LDAP://ou=AdminNotbooks, ou=Notebooks,dc=YourDomain,dc=com
This would become very difficult for me to do this for 4000+ machines.Is there a way that i mention the Ou path in the script.
So that i can run the script 4 times for 4 locations.

I have the machine names in each location seperately.
yes thats possible! So just list the PC's in a text file
This should do the Job:
'------------------------------------------------------------------------------------------------------
On Error Resume Next
dim objFSO, objFile
dim currentPC

set objFSO = CreateObject("Scripting.FileSystemObject")
set objFile = objFSO.OpenTextFile("C:\YourPCs.txt)    'Change here the path to the textfile

Const ADS_SCOPE_SUBTREE = 2
Set objConnection = CreateObject("ADODB.Connection")
Set objCommand =   CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
Set objCommand.ActiveConnection = objConnection

objCommand.Properties("Page Size") = 1000
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE


do while not objFile.AtEndOfStream
      currentPC = objFile.ReadLine()

      objOU = GetObject("LDAP://ou=YourOU,dc=YourDomain,dc=com") ' Change the specified OU and Domain here


      objCommand.CommandText = "SELECT ADsPath FROM 'LDAP://dc=YourDomain,dc=com' WHERE objectCategory='computer' AND Name='" &CurrentPC &"'"
      Set objRecordSet = objCommand.Execute

      objRecordSet.MoveFirst

      Do Until objRecordSet.EOF
               strADsPath = objRecordSet.Fields("ADsPath").Value
               objOU.MoveHere strADsPath, vbNullString
               objRecordSet.MoveNext
      Loop
loop
'------------------------------------------------------------------------------------------------------

Note first make test with less PC's :)
And test the Script in a little envionment!
Try this:

:: ===============
:: READ THIS FIRST
:: ===============
:: * This script Computers.txt file on C: drive root, from where it will pick computer names.
:: * You need to set "TargetDN" variables value to the new OU where you want to move computer
::    Like:
::             SET TargetDN=OU=Accounts,DC=Training,DC=Com
::
:: * Successful run will generate "CompMoveReport.txt" file on C: drive root.
:: * Copy and paste following script in notepad and save it with any name having .bat extension.
::
:: *** SCRIPT START ***
@Echo Off
SETLOCAL EnableDelayedExpansion
SET TargetDN=OU=Accounts,DC=Training,DC=Com

IF NOT EXIST C:\Computers.txt Goto ShowErr
FOR %%R IN (C:\Computers.txt) Do IF %%~zR EQU 0 Goto ShowErr
IF EXIST C:\CompMoveReport.txt DEL /F /Q C:\CompMoveReport.txt

FOR /F "delims=#" %%c IN ('Type C:\Computers.txt') Do (
      Echo Processing: %%c
        DSQuery Computer -Name %%c | DSMove -NewParent "%TargetDN%" >>C:\CompMoveReport.txt 2>&1
)

Goto EndScript
:ShowErr
Echo 'C:\Computers.txt' file does not exist or file is empty!
:EndScript
ENDLOCAL
:: *** SCRIPT END ***
Ohoh I think I answered to the wrong question! above script will move all machines inside the file to the target OU (given in TargetDN variable)
ASKER CERTIFIED SOLUTION
Avatar of Farhan Kazi
Farhan Kazi
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
merowinger:
I get this...
---------------------------
Windows Script Host
---------------------------
Script:      C:\Move Computers to Ou in list.vbs
Line:      6
Char:      92
Error:      Unterminated string constant
Code:      800A0409
Source:       Microsoft VBScript compilation error

---------------------------
OK  
---------------------------
farhankazi
What will Your first script do?
What will your last script do?



1) Ignore my first script (it basically move computers to the ou, that you specify inside script)
2) Second script will sort machine names (in a file) as per its OU.
farhankazi
Second script will sort machine names (in a file) as per its OU.
sorry did not understand
oh i forgot a " sign in line 6:
set objFile = objFSO.OpenTextFile("C:\YourPCs.txt)

so the right syntax is:

set objFile = objFSO.OpenTextFile("C:\YourPCs.txt")
Sharath:
Lets say you have following computers in a file like:
PC1
PC7
PC6
PC4
PC2
PC5
PC3

Now above script will sort them according to their OU's
Like:
----------
ACCOUNTS
----------
PC1
PC7
PC6
PC3

----------
SALES:
----------
PC4
PC2
PC5