Sign in to follow this  
Followers 0
Gunbuster

Help with script to move computers in AD

4 posts in this topic

I am currently using a batch file in combination with a VB Script to move computers from the default directory where they go when being added to the domain. I would like to rewrite this in AutoIt so that it has a more user friendly interface.

Here is the VB Script I have been using. (I didn't write this. It was written by Chimera Computing Ltd)

Option Explicit 

' Declare variables 
dim objOU, objPC, objRootDSE, strDNSDomain 
dim sSourceCN, sDestCN, objNet, sThisComputer 

' Find this computer name 
set objNet = CreateObject("WScript.NetWork") 
sThisComputer = objNet.ComputerName 
 
' Get AD details 
set objRootDSE = GetObject("LDAP://RootDSE") 
strDNSDomain = objRootDSE.Get("DefaultNamingContext") 

' Set query source and destination 
sSourceCN = "LDAP://CN=" & sThisComputer & ",OU=Computers," & strDNSDomain 
sDestCN = "LDAP://OU=Division,OU=Region,OU=City,OU=Computers," & strDNSDomain 

' Move computer object 
set objOU = GetObject(sDestCN) 
WScript.echo "Moving computer account" 
set objPC = objOU.MoveHere(sSourceCN, vbNullString)
WScript.echo & sThisComputer $ " has successfully been moved to " & sDestCN & vbNewline

I am new to AutoIt and have VERY LITTLE knowledge of VB Scripting. Any help with this is appreciated.

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

I am currently using a batch file in combination with a VB Script to move computers from the default directory where they go when being added to the domain. I would like to rewrite this in AutoIt so that it has a more user friendly interface.

Here is the VB Script I have been using. (I didn't write this. It was written by Chimera Computing Ltd)

Option Explicit 

' Declare variables 
dim objOU, objPC, objRootDSE, strDNSDomain 
dim sSourceCN, sDestCN, objNet, sThisComputer 

' Find this computer name 
set objNet = CreateObject("WScript.NetWork") 
sThisComputer = objNet.ComputerName 
 
' Get AD details 
set objRootDSE = GetObject("LDAP://RootDSE") 
strDNSDomain = objRootDSE.Get("DefaultNamingContext") 

' Set query source and destination 
sSourceCN = "LDAP://CN=" & sThisComputer & ",OU=Computers," & strDNSDomain 
sDestCN = "LDAP://OU=Division,OU=Region,OU=City,OU=Computers," & strDNSDomain 

' Move computer object 
set objOU = GetObject(sDestCN) 
WScript.echo "Moving computer account" 
set objPC = objOU.MoveHere(sSourceCN, vbNullString)
WScript.echo & sThisComputer $ " has successfully been moved to " & sDestCN & vbNewline

I am new to AutoIt and have VERY LITTLE knowledge of VB Scripting. Any help with this is appreciated.

Welcome to these forums .

There are many posts on AD stuff .. just search and see if anything is useful for you to get you started.

Moved to the correct Support forum . :)

Jos

Edited by Jos

Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

I am currently using a batch file in combination with a VB Script to move computers from the default directory where they go when being added to the domain. I would like to rewrite this in AutoIt so that it has a more user friendly interface.

Here is the VB Script I have been using. (I didn't write this. It was written by Chimera Computing Ltd)

Option Explicit 

' Declare variables 
dim objOU, objPC, objRootDSE, strDNSDomain 
dim sSourceCN, sDestCN, objNet, sThisComputer 

' Find this computer name 
set objNet = CreateObject("WScript.NetWork") 
sThisComputer = objNet.ComputerName 
 
' Get AD details 
set objRootDSE = GetObject("LDAP://RootDSE") 
strDNSDomain = objRootDSE.Get("DefaultNamingContext") 

' Set query source and destination 
sSourceCN = "LDAP://CN=" & sThisComputer & ",OU=Computers," & strDNSDomain 
sDestCN = "LDAP://OU=Division,OU=Region,OU=City,OU=Computers," & strDNSDomain 

' Move computer object 
set objOU = GetObject(sDestCN) 
WScript.echo "Moving computer account" 
set objPC = objOU.MoveHere(sSourceCN, vbNullString)
WScript.echo & sThisComputer $ " has successfully been moved to " & sDestCN & vbNewline

I am new to AutoIt and have VERY LITTLE knowledge of VB Scripting. Any help with this is appreciated.

Here's a conversion. Not tested because I'm not with my domains right now:

Opt("MustDeclareVars", 1)

Global $objOU, $objPC, $objRootDSE, $strDNSDomain
Global $sSourceCN, $sDestCN, $objNet, $sThisComputer, $sMsg

; Find this computer name
$objNet = ObjCreate("WScript.NetWork")
$sThisComputer = $objNet.ComputerName

; Get AD details
$objRootDSE = ObjGet("LDAP://RootDSE")
$strDNSDomain = $objRootDSE.Get("DefaultNamingContext")

; Set query source and destination
$sSourceCN = "LDAP://CN=" & $sThisComputer & ",OU=Computers," & $strDNSDomain
$sDestCN = "LDAP://OU=Division,OU=Region,OU=City,OU=Computers," & $strDNSDomain

; Move computer object
$objOU = ObjGet($sDestCN)
$objPC = $objOU.MoveHere($sSourceCN, "")

; Report results
$sMsg = "Moving computer account:" & @CRLF & _
        $sThisComputer & " has successfully been moved to " & $sDestCN
MsgBox(64, "Results", $sMsg)

Cheers!

:)


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

What about if the account you are using does not have rights to move computers. Can you add a line in the script to use different credentials? For instance to move the account when it is in the local admin account of the computer.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0