We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

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

Medium Priority
428 Views
Last Modified: 2010-03-05
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
Comment
Watch Question

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

Author

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.
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

Author

Commented:
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

Author

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.
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!
Farhan KaziSystems Engineer
CERTIFIED EXPERT
Top Expert 2007

Commented:
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 ***
Farhan KaziSystems Engineer
CERTIFIED EXPERT
Top Expert 2007

Commented:
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)
Systems Engineer
CERTIFIED EXPERT
Top Expert 2007
Commented:
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 ***

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

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  
---------------------------

Author

Commented:
farhankazi
What will Your first script do?
What will your last script do?



Farhan KaziSystems Engineer
CERTIFIED EXPERT
Top Expert 2007

Commented:
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.

Author

Commented:
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")
Farhan KaziSystems Engineer
CERTIFIED EXPERT
Top Expert 2007

Commented:
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
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.