Solved Script to parse .txt & move Computer Accounts to different OU

re: Script to parse .txt & move Computer Accounts to different OU

Try this, report back :) I probably have not got it right first time, but have corrected two syntax errors.

Option Explicit

Dim strFile, strTargetOU, objFSO, objFile, objOU

Dim objRootDSE, strDNSDomain, objTrans, strNetBIOSDomain

Dim strComputer, strComputerDN, objComputer

Const ADS_NAME_INITTYPE_DOMAIN = 1

Const ADS_NAME_TYPE_NT4 = 3

Const ADS_NAME_TYPE_1179 = 1

' Specify text file of computer NetBIOS names.

strFile = "c:\Computers.txt"

' Specify target OU to move computer objects into.

strTargetOU = "ou=SrAccounts,dc=MyDomain,dc=com"

' Open the file for read access.

Set objFSO = CreateObject("Scripting.FileSystemObject")

Set objFile = objFSO.OpenTextFile(strFile, 1)

' Bind to the target OU.

Set objOU = GetObject("LDAP://" & strTargetOU)

' Determine DNS domain name from RootDSE object.

Set objRootDSE = GetObject("LDAP://RootDSE")

strDNSDomain = objRootDSE.Get("DefaultNamingContext")

' Use the NameTranslate object to find the NetBIOS domain name from

' the DNS domain name.

Set objTrans = CreateObject("NameTranslate")

objTrans.Init ADS_NAME_TYPE_NT4, strDNSDomain

objTrans.Set ADS_NAME_TYPE_1179, strDNSDomain

strNetBIOSDomain = objTrans.Get(ADS_NAME_TYPE_NT4)

' Remove trailing backslash.

strNetBIOSDomain = Left(strNetBIOSDomain, Len(strNetBIOSDomain) - 1)

' Use the NameTranslate object to convert computer NetBIOS

' names to Distinguished Names.

objTrans.Init ADS_NAME_INITTYPE_DOMAIN, strNetBIOSDomain

' Read lines from the file.

Do Until objFile.AtEndOfStream

strComputer = Trim(objFile.ReadLine)

If (strComputer <> "") Then

' Convert NetBIOS name to DN.

' NetBIOS name must have "$" appended to end.

' Trap error if computer not found.

On Error Resume Next

objTrans.Set ADS_NAME_TYPE_NT4, strNetBIOSDomain & "\" & strComputer & "$"

If (Err.Number <> 0) Then

On Error Goto 0

Wscript.Echo "Computer not found: " & strComputer

Else

On Error GoTo 0

strComputerDN = objTrans.Get(ADS_NAME_TYPE_1179)

' Bind to the computer object.

Set objComputer = GetObject("LDAP://" & strComputerDN)

' Move the computer object to the target OU.

objTargetOU.MoveHere objComputer.AdsPath, vbNullString

End If

End If

Loop

Wscript.Echo "Done"
 

My Computer

System One

  • Manufacturer/Model
    Dell XPS 420
    CPU
    Intel Core 2 Quad Q9300 2.50GHz
    Motherboard
    Stock Dell 0TP406
    Memory
    4 gb (DDR2 800) 400MHz
    Graphics Card(s)
    ATI Radeon HD 3870 (512 MBytes)
    Sound Card
    Onboard
    Monitor(s) Displays
    1 x Dell 2007FP and 1 x (old) Sonic flat screen
    Screen Resolution
    1600 x 1200 and 1280 x 1204
    Hard Drives
    1 x 640Gb (SATA 300)
    Western Digital: WDC WD6400AAKS-75A7B0

    1 x 1Tb (SATA 600)
    Western Digital: Caviar Black, SATA 6GB/S, 64Mb cache, 8ms
    Western Digital: WDC WD1002FAEX-00Z3A0 ATA Device
    PSU
    Stock PSU - 375W
    Case
    Dell XPS 420
    Cooling
    Stock Fan
    Keyboard
    Dell Bluetooth
    Mouse
    Advent Optical ADE-WG01 (colour change light up)
    Internet Speed
    120 kb/s
    Other Info
    ASUS USB 3.0 5Gbps/SATA 6Gbps - PCI-Express Combo Controller Card (U3S6)
re: Script to parse .txt & move Computer Accounts to different OU

Thanks for the quick reply!!

Now I'm getting this:

Line: 97
Char: 1
Error: Variable is undefined: 'objTargetOU'

Obviously, that means there is no declaration. However, since that is the only instance of objTargetOU in the script, I'm not sure if that is supposed to be in the Dim or not.
 

My Computer

re: Script to parse .txt & move Computer Accounts to different OU

Thanks for the quick reply!!

Now I'm getting this:

Line: 97
Char: 1
Error: Variable is undefined: 'objTargetOU'

Obviously, that means there is no declaration. However, since that is the only instance of objTargetOU in the script, I'm not sure if that is supposed to be in the Dim or not.

I hate VB with a passion. What is going on in this script exactly? Do you think that you are supposed to add it into the Dim manually, based on the name of the computer to move to, which the original code writer could not know, or have you put that somewhere in the text file? That would be my guess: you are meant to put it in the Dim yourself, with a value. What do you think? I am useless with VBScript. Trying to put DirectX knowledge into VBScript. Hmmm.
 

My Computer

System One

  • Manufacturer/Model
    Dell XPS 420
    CPU
    Intel Core 2 Quad Q9300 2.50GHz
    Motherboard
    Stock Dell 0TP406
    Memory
    4 gb (DDR2 800) 400MHz
    Graphics Card(s)
    ATI Radeon HD 3870 (512 MBytes)
    Sound Card
    Onboard
    Monitor(s) Displays
    1 x Dell 2007FP and 1 x (old) Sonic flat screen
    Screen Resolution
    1600 x 1200 and 1280 x 1204
    Hard Drives
    1 x 640Gb (SATA 300)
    Western Digital: WDC WD6400AAKS-75A7B0

    1 x 1Tb (SATA 600)
    Western Digital: Caviar Black, SATA 6GB/S, 64Mb cache, 8ms
    Western Digital: WDC WD1002FAEX-00Z3A0 ATA Device
    PSU
    Stock PSU - 375W
    Case
    Dell XPS 420
    Cooling
    Stock Fan
    Keyboard
    Dell Bluetooth
    Mouse
    Advent Optical ADE-WG01 (colour change light up)
    Internet Speed
    120 kb/s
    Other Info
    ASUS USB 3.0 5Gbps/SATA 6Gbps - PCI-Express Combo Controller Card (U3S6)
re: Script to parse .txt & move Computer Accounts to different OU

I fixed it from your changes. I changed objTargetOU to objOU which was already declared and it works. Thanks for getting me passed the initial error, I would have pulled out the hairs on my chest trying to figure this one out...I'm already bald from other scripts. :-)
 

My Computer

re: Script to parse .txt & move Computer Accounts to different OU

I fixed it from your changes. I changed objTargetOU to objOU which was already declared and it works. Thanks for getting me passed the initial error, I would have pulled out the hairs on my chest trying to figure this one out...I'm already bald from other scripts. :-)

Well done! I did wonder whether it was something like this, but I did not have a good enough knowledge of the script to work it out, so I made some other suggestions :o Really glad that the problem has been fixed!

Richard
 

My Computer

System One

  • Manufacturer/Model
    Dell XPS 420
    CPU
    Intel Core 2 Quad Q9300 2.50GHz
    Motherboard
    Stock Dell 0TP406
    Memory
    4 gb (DDR2 800) 400MHz
    Graphics Card(s)
    ATI Radeon HD 3870 (512 MBytes)
    Sound Card
    Onboard
    Monitor(s) Displays
    1 x Dell 2007FP and 1 x (old) Sonic flat screen
    Screen Resolution
    1600 x 1200 and 1280 x 1204
    Hard Drives
    1 x 640Gb (SATA 300)
    Western Digital: WDC WD6400AAKS-75A7B0

    1 x 1Tb (SATA 600)
    Western Digital: Caviar Black, SATA 6GB/S, 64Mb cache, 8ms
    Western Digital: WDC WD1002FAEX-00Z3A0 ATA Device
    PSU
    Stock PSU - 375W
    Case
    Dell XPS 420
    Cooling
    Stock Fan
    Keyboard
    Dell Bluetooth
    Mouse
    Advent Optical ADE-WG01 (colour change light up)
    Internet Speed
    120 kb/s
    Other Info
    ASUS USB 3.0 5Gbps/SATA 6Gbps - PCI-Express Combo Controller Card (U3S6)
Back
Top