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
LVL 11
bsharathAsked:
Who is Participating?
 
Farhan KaziConnect With a Mentor Systems EngineerCommented:
Check following, it will sort computers names as per its OU

:: ===============
:: READ THIS FIRST
:: ===============
:: * This script Computers.txt file on C: drive root, from where it will pick computer names.
:: * Successful run will generate "SortedComp.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

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

FOR /F "delims=#" %%c IN ('Type C:\Computers.txt') Do (
      Echo Processing: %%c
        SET Qry=DSQuery Computer -Name %%c ^| Find "CN="
        FOR /F "delims=, tokens=2" %%o IN ('!Qry!') Do Echo %%o#%%c >>C:\TmpCList.txt
)
Echo.
Echo Sorting list please wait....
Echo.
Sort C:\TmpCList.txt /O C:\SortedCList.txt
SET OUName=
FOR /F "delims=# tokens=1-2" %%a IN ('Type C:\SortedCList.txt') Do (
      IF NOT "!OUName!"=="%%a" (
            Echo ------------------->>C:\SortedComp.txt
            Echo %%a >>C:\SortedComp.txt
            Echo ------------------->>C:\SortedComp.txt
      )
      Echo %%b >>C:\SortedComp.txt
      SET OUName=%%a
)
Echo Sorting Complete! Check C:\SortedComp.txt file.
Echo.
Goto EndScript
:ShowErr
Echo 'C:\Computers.txt' file does not exist or file is empty!
:EndScript
IF EXIST C:\TmpCList.txt DEL /F /Q C:\TmpCList.txt
IF EXIST C:\SortedCList.txt DEL /F /Q C:\SortedCList.txt
ENDLOCAL
:: *** SCRIPT END ***

0
 
merowingerCommented:
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
0
 
bsharathAuthor Commented:
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.
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

 
merowingerCommented:
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
0
 
bsharathAuthor Commented:
Which are the paths i need to make changes according to my domain
0
 
merowingerCommented:
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
0
 
bsharathAuthor Commented:
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.
0
 
merowingerCommented:
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
'------------------------------------------------------------------------------------------------------

0
 
merowingerCommented:
Note first make test with less PC's :)
And test the Script in a little envionment!
0
 
Farhan KaziSystems EngineerCommented:
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 ***
0
 
Farhan KaziSystems EngineerCommented:
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)
0
 
bsharathAuthor Commented:
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  
---------------------------
0
 
bsharathAuthor Commented:
farhankazi
What will Your first script do?
What will your last script do?



0
 
Farhan KaziSystems EngineerCommented:
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.
0
 
bsharathAuthor Commented:
farhankazi
Second script will sort machine names (in a file) as per its OU.
sorry did not understand
0
 
merowingerCommented:
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")
0
 
Farhan KaziSystems EngineerCommented:
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
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.