Given a list of domain computers - how can I script the placement of these computers into a specific

Discussion in 'Active Directory' started by Spin, Aug 30, 2006.

  1. Spin

    Spin Guest


    Given a list of domain computers - how can I script the placement of these
    computers into a specific OU?
    Spin, Aug 30, 2006
    1. Advertisements

  2. You want to place them in specific OU?
    If this is a case You have to perform move operation on AD object

    You have to get OU object (objOU) and computer object (objComputer) and
    then perform MoveHere operation:
    objOU.MoveHere objComputer.AdsPath, vbNullString
    Tomasz Onyszko, Aug 30, 2006
    1. Advertisements

  3. Hi,

    You can use the FileSystem object to read the text file. Assuming your text
    file has the NetBIOS names of the computers, you can use the NameTranslate
    object to convert this (with the NetBIOS name of the domain) to the
    Distinguished Name required for the LDAP provider. As noted, you bind to the
    OU and invoke the MoveHere method. You pass the AdsPath of the computer
    object to the method. For example:
    Option Explicit

    Dim objNewOU, strFile, objFSO, objFile, objTrans
    Dim strComputer, strComputerDN, strDomain

    Const ForReading = 1
    Const ADS_NAME_TYPE_NT4 = 3
    Const ADS_NAME_TYPE_1179 = 1

    ' Specify the NetBIOS name of the domain
    strDomain = "MyDomain"

    ' Bind to target OU, where computer objects will be moved.
    Set objNewOU = GetObject("LDAP://OU=OldComputers,dc=MyDomain,dc=com")

    ' Specify file of NetBIOS names of computers to move.
    strFile = "c:\scripts\computers.txt"

    ' Use NameTranslate object to convert NetBIOS names to
    ' Distinguished Names.
    Set objTrans = CreateObject("NameTranslate")

    ' Initialize NameTranslate by locating Global Catalog.
    objTrans.Init ADS_NAME_INITTYPE_GC, ""

    ' Open file for read access.
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objFile = objFSO.OpenTextFile(strFile, ForReading)

    ' Read each line of the file.
    Do Until objFile.AtEndOfStream
    strComputer = Trim(objFile.ReadLine)
    ' Skip blank lines.
    If (strComputer <> "") Then
    ' Specify the domain and NT name of the computer.
    ' The NT name is the NetBIOS name with "$" appended.
    objTrans.Set ADS_NAME_TYPE_NT4, strDomain & "\" & strComputer & "$"
    ' Retrieve the Distinguished Name.
    strComputerDN = objTrans.Get(ADS_NAME_TYPE_1179)
    ' Move the computer.
    objNewOU.MoveHere "LDAP://" & strComputerDN, vbNullString
    End If

    ' Clean up.
    Set objFile = Nothing
    Set objFSO = Nothing
    Set objNewOU = Nothing
    Set objTrans = Nothing
    Richard Mueller, Aug 31, 2006
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.