?
Solved

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

Posted on 2007-07-20
17
Medium Priority
?
381 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
0
Comment
Question by:bsharath
  • 6
  • 6
  • 5
17 Comments
 
LVL 31

Expert Comment

by:merowinger
ID: 19529456
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
 
LVL 11

Author Comment

by:bsharath
ID: 19529521
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
 
LVL 31

Expert Comment

by:merowinger
ID: 19529536
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 11

Author Comment

by:bsharath
ID: 19529583
Which are the paths i need to make changes according to my domain
0
 
LVL 31

Expert Comment

by:merowinger
ID: 19529629
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
 
LVL 11

Author Comment

by:bsharath
ID: 19529668
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
 
LVL 31

Expert Comment

by:merowinger
ID: 19529705
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
 
LVL 31

Expert Comment

by:merowinger
ID: 19529707
Note first make test with less PC's :)
And test the Script in a little envionment!
0
 
LVL 26

Expert Comment

by:Farhan Kazi
ID: 19533653
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
 
LVL 26

Expert Comment

by:Farhan Kazi
ID: 19533719
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
 
LVL 26

Accepted Solution

by:
Farhan Kazi earned 2000 total points
ID: 19534330
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
 
LVL 11

Author Comment

by:bsharath
ID: 19545166
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
 
LVL 11

Author Comment

by:bsharath
ID: 19545173
farhankazi
What will Your first script do?
What will your last script do?



0
 
LVL 26

Expert Comment

by:Farhan Kazi
ID: 19545199
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
 
LVL 11

Author Comment

by:bsharath
ID: 19545215
farhankazi
Second script will sort machine names (in a file) as per its OU.
sorry did not understand
0
 
LVL 31

Expert Comment

by:merowinger
ID: 19545706
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
 
LVL 26

Expert Comment

by:Farhan Kazi
ID: 19545763
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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Welcome to my series of short tips on migrations. Whilst based on Microsoft migrations the same principles can be applied to any type of migration. My first tip Migration Tip #1 – Source Server Health can be found here: http://www.experts-exchang…
Citrix XenApp, Internet Explorer 11 set to Enterprise Mode and using central hosted sites.xml file.
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…
Suggested Courses

840 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