Jump to content

Move a machine in AD


Emolas
 Share

Recommended Posts

First off I would like to thank all the helpful people on these boards, I have learned so much just by reading other people's questions and answers. I'm a super newb at AutoIT (This is my first script) but have an understanding of programming (BASIC, assembly, and COBOL from the 80's along with a smattering of VB).

Now on to my question: I need to move a computer from one OU to another within the same domain.

for example: I want to move CN=bobsmachine,OU=Computers,OU=London,OU=anotherOU,OU=lastOU,DC=testdomain,DC=net

to CN=bobsmachine,OU=Computers,OU=Paris,OU=anotherOU,OU=lastOU,DC=testdomain,DC=net

Edited to remove corporate AD structure and to remove use of .ini files but essentially it's the same.

#include <Constants.au3>
#include <GUIConstants.au3>
#include <ComboConstants.au3>
#include <EditConstants.au3>
#include <String.au3>

Global $PCType, $DomainJoin, $Local, $Remote, $ComputerName, $domain, $SiteCombo, $i, $Varw, $Switch, $Confirm 
Global $objTrans, $objDomain, $Sou, $GetDN, $NewDN, $sSourceDN, $sDestCN
$wshShell = ObjCreate("WScript.Shell")
$wshNetwork = ObjCreate("WScript.Network")

;################################################### GUI
    opt("GUIOnEventMode", 1)
; Build it
    $DomainJoin = GUICreate("Domain Join ",700, 200, 170, 115)
    GUICtrlCreateLabel("Are you switching this computer or a remote computer?", 75, 10, 650, 30)
    GUICtrlSetFont(-1, 15, 800, 0, "MS Sans Serif")
    GUICtrlCreateLabel("This computer:", 75, 43, 100)
    GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
    $Local = GUICtrlCreateButton(@ComputerName,195, 40)
    GUICtrlCreateLabel("A remote computer:", 350, 43, 200)
    GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
    $Remote = GUICtrlCreateButton("Remote",485, 40, 100)
    GUISetState(@SW_SHOW)
; Read Clicks
    GUICtrlSetOnEvent($Local, "GetDN")
    GUICtrlSetOnEvent($Remote, "SetRemote")
    GUISetOnEvent($GUI_EVENT_ClOSE,"ExitMessage")
; Do nothing for a while
    While 1
        Sleep(500)
    WEnd

;##################################################### Get the remote machine name
Func SetRemote()
    $ComputerName = InputBox("Remote Machine", "Enter Machine Name", "")
    if $ComputerName <> "" Then     
        GetDN()
    EndIf   
EndFunc

;#################################################### Get the current DN
Func GetDN()
; assign computer name to local machine if applicable
if @GUI_CtrlId = $Local Then
    $ComputerName= @ComputerName
EndIf
; Constants for the NameTranslate object.
Const $ADS_NAME_INITTYPE_GC = 3
Const $ADS_NAME_TYPE_NT4 = 3
Const $ADS_NAME_TYPE_1779 = 1
; Grab the current full name and path   
$objTrans = ObjCreate("NameTranslate")
$objDomain = Objget("LDAP://rootDse")
$objTrans.Init ($ADS_NAME_INITTYPE_GC, "")
$objTrans.Set ($ADS_NAME_TYPE_NT4, $wshNetwork.UserDomain & "\" & $ComputerName & "$")
$GetDN = $objTrans.Get($ADS_NAME_TYPE_1779)
;display current computer info and create combo to select new site
GUICtrlCreateLabel("Current DN: " & $GetDN, 10, 110, 650, 100)
GUICtrlCreateLabel("PC Type: " & $PCType & "   Domain: " & $domain, 10, 125)
GUICtrlCreateLabel("Select new site: ", 10, 77)
$SiteCombo = GUICtrlCreateCombo("", 110, 75, 50, 25,$CBS_DROPDOWNLIST)
GUICtrlSetData(-1, "NewYork|London|Paris|Berlin|Sofia")
GUICtrlSetOnEvent($SiteCombo, "NewSite")
EndFunc

;######################################################### Set new site OU
Func NewSite()
$Site = GUICtrlRead($SiteCombo)
$SOU = "Computers,OU=" & $Site
$NewDN = "OU="& $SOU & ",OU=FirstOU,OU=SecondOU,DC=testdomain,DC=net"
GUICtrlCreateLabel("New DN:  " & "CN=" & $ComputerName & "," & $NewDN, 10, 150, 650, 100)
$Switch = GUICtrlCreateButton("MAKE THE SWITCH", 195, 75, 150, -1)
    GUICtrlSetOnEvent($Switch, "Confirm")
EndFunc

;########################################################## Confirm change
Func Confirm()
    $Confirm = MsgBox(1, "Confirm Switch", "Are you certain you want to move " & $ComputerName & " to the " & $Sou & " OU?")
    if $Confirm = 1 Then
        MoveIt()
    EndIf
EndFunc

;########################################################### move PC to new OU
Func MoveIt()
; Set query source and destination
$SourceCN = "LDAP://" & $GetDN
$DestCN = "LDAP://" & $NEWDN
; Set Target OU
$objCont = ObjGet($DestCN)
; Move the Computer into Target OU
$objPC = $objCont.MoveHere($SourceDN)
EndFunc

;############################################################ Exit button pushed
Func ExitMessage()
    Exit
EndFunc

I think my whole problem is not knowing the syntax for "$objCont.MoveHere($sSourceDN)" What must those variables contain?

C:\Documents and Settings\john.salome\Desktop\script\MoveBuilding.au3 (148) : ==> The requested action with this object has failed.:

$objPC = $objCont.MoveHere($sSourceDN)

$objPC = $objCont.MoveHere($sSourceDN)^ ERROR

Link to comment
Share on other sites

Haven't done it myself but could you have a look at the adfunctions.au3 UDF? In function _ADRenameObject you could find some hints.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Well I couldn't figure out how to do it using _ADFunctions.au3.

However, this post was EXTREMELY helpful and I was able cut out several lines and replaced them with this little gem: $run = @ComSpec & " /c dsmove.exe """& $OldDN &""" -newparent """ & $NewDN & """ -u !"& @UserName & " -p " & $password

Link to comment
Share on other sites

First off I would like to thank all the helpful people on these boards, I have learned so much just by reading other people's questions and answers. I'm a super newb at AutoIT (This is my first script) but have an understanding of programming (BASIC, assembly, and COBOL from the 80's along with a smattering of VB).

Now on to my question: I need to move a computer from one OU to another within the same domain.

for example: I want to move CN=bobsmachine,OU=Computers,OU=London,OU=anotherOU,OU=lastOU,DC=testdomain,DC=net

to CN=bobsmachine,OU=Computers,OU=Paris,OU=anotherOU,OU=lastOU,DC=testdomain,DC=net

Edited to remove corporate AD structure and to remove use of .ini files but essentially it's the same.

#include <Constants.au3>
#include <GUIConstants.au3>
#include <ComboConstants.au3>
#include <EditConstants.au3>
#include <String.au3>

Global $PCType, $DomainJoin, $Local, $Remote, $ComputerName, $domain, $SiteCombo, $i, $Varw, $Switch, $Confirm 
Global $objTrans, $objDomain, $Sou, $GetDN, $NewDN, $sSourceDN, $sDestCN
$wshShell = ObjCreate("WScript.Shell")
$wshNetwork = ObjCreate("WScript.Network")

;################################################### GUI
    opt("GUIOnEventMode", 1)
; Build it
    $DomainJoin = GUICreate("Domain Join ",700, 200, 170, 115)
    GUICtrlCreateLabel("Are you switching this computer or a remote computer?", 75, 10, 650, 30)
    GUICtrlSetFont(-1, 15, 800, 0, "MS Sans Serif")
    GUICtrlCreateLabel("This computer:", 75, 43, 100)
    GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
    $Local = GUICtrlCreateButton(@ComputerName,195, 40)
    GUICtrlCreateLabel("A remote computer:", 350, 43, 200)
    GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
    $Remote = GUICtrlCreateButton("Remote",485, 40, 100)
    GUISetState(@SW_SHOW)
; Read Clicks
    GUICtrlSetOnEvent($Local, "GetDN")
    GUICtrlSetOnEvent($Remote, "SetRemote")
    GUISetOnEvent($GUI_EVENT_ClOSE,"ExitMessage")
; Do nothing for a while
    While 1
        Sleep(500)
    WEnd

;##################################################### Get the remote machine name
Func SetRemote()
    $ComputerName = InputBox("Remote Machine", "Enter Machine Name", "")
    if $ComputerName <> "" Then     
        GetDN()
    EndIf   
EndFunc

;#################################################### Get the current DN
Func GetDN()
; assign computer name to local machine if applicable
if @GUI_CtrlId = $Local Then
    $ComputerName= @ComputerName
EndIf
; Constants for the NameTranslate object.
Const $ADS_NAME_INITTYPE_GC = 3
Const $ADS_NAME_TYPE_NT4 = 3
Const $ADS_NAME_TYPE_1779 = 1
; Grab the current full name and path   
$objTrans = ObjCreate("NameTranslate")
$objDomain = Objget("LDAP://rootDse")
$objTrans.Init ($ADS_NAME_INITTYPE_GC, "")
$objTrans.Set ($ADS_NAME_TYPE_NT4, $wshNetwork.UserDomain & "\" & $ComputerName & "$")
$GetDN = $objTrans.Get($ADS_NAME_TYPE_1779)
;display current computer info and create combo to select new site
GUICtrlCreateLabel("Current DN: " & $GetDN, 10, 110, 650, 100)
GUICtrlCreateLabel("PC Type: " & $PCType & "   Domain: " & $domain, 10, 125)
GUICtrlCreateLabel("Select new site: ", 10, 77)
$SiteCombo = GUICtrlCreateCombo("", 110, 75, 50, 25,$CBS_DROPDOWNLIST)
GUICtrlSetData(-1, "NewYork|London|Paris|Berlin|Sofia")
GUICtrlSetOnEvent($SiteCombo, "NewSite")
EndFunc

;######################################################### Set new site OU
Func NewSite()
$Site = GUICtrlRead($SiteCombo)
$SOU = "Computers,OU=" & $Site
$NewDN = "OU="& $SOU & ",OU=FirstOU,OU=SecondOU,DC=testdomain,DC=net"
GUICtrlCreateLabel("New DN:  " & "CN=" & $ComputerName & "," & $NewDN, 10, 150, 650, 100)
$Switch = GUICtrlCreateButton("MAKE THE SWITCH", 195, 75, 150, -1)
    GUICtrlSetOnEvent($Switch, "Confirm")
EndFunc

;########################################################## Confirm change
Func Confirm()
    $Confirm = MsgBox(1, "Confirm Switch", "Are you certain you want to move " & $ComputerName & " to the " & $Sou & " OU?")
    if $Confirm = 1 Then
        MoveIt()
    EndIf
EndFunc

;########################################################### move PC to new OU
Func MoveIt()
; Set query source and destination
$SourceCN = "LDAP://" & $GetDN
$DestCN = "LDAP://" & $NEWDN
; Set Target OU
$objCont = ObjGet($DestCN)
; Move the Computer into Target OU
$objPC = $objCont.MoveHere($SourceDN)
EndFunc

;############################################################ Exit button pushed
Func ExitMessage()
    Exit
EndFunc

I think my whole problem is not knowing the syntax for "$objCont.MoveHere($sSourceDN)" What must those variables contain?

C:\Documents and Settings\john.salome\Desktop\script\MoveBuilding.au3 (148) : ==> The requested action with this object has failed.:

$objPC = $objCont.MoveHere($sSourceDN)

$objPC = $objCont.MoveHere($sSourceDN)^ ERROR

Hi,

$objPC isn't an object, isn't it? So you get the error above.

This should work:

Func MoveIt()
; Set query source and destination
$SourceCN = "LDAP://" & $GetDN
$DestCN = "LDAP://" & $NEWDN
; Set Target OU
$objCont = ObjGet($DestCN)
; Move the Computer into Target OU
$objCont.MoveHere($SourceDN)
EndFunc

If you get com error try add this at beginning of script:

Local $oVBS = ObjCreate("ScriptControl")
$oVBS.language = "VBScript"
Global Const $NULL = $oVBS.eval("vbNullString")
$oVBS = 0

and change

$objCont.MoveHere($SourceDN) to $objCont.MoveHere($SourceDN, $NULL)

;-))

Stefan

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...