Script to move a machine in AD after adding it to the domain

This is probably the script of which I’m most proud. Although I haven’t used it at many employers, it took a lot of testing to get it working.

This script checks the Computers container in Active Directory for machines that have recently been joined to the domain. It then prompts the user to decide whether the machine should be moved, if the response is yes then the machine is moved into the correct Organisational Unit.

You will need to edit line 20 to ensure you have entered the required information for your domain, as well as 40 and 45 to include the correct OUs for your machines.

This script was created on a domain which contained Windows XP and Vista machines, it can easily be extended to include other operating systems. You will need sufficient permission on the domain for the account you run this script from.

'**************************************************************************************************
'*Script Name: MoveMachine.vbs
'*Created on:  23/02/2009
'*Author:      Tom Anderson
'*Purpose:     This script checks the Computers container in Active Directory for machines that 
'*             have recently been joined to the domain. It then prompts the user to decide whether
'*             the machine should be moved, if the response is yes then the machine is moved
'*             into the correct Organisation Unit.
'*History:     27/02/2009 - Performed check for OS, removing user input.
'*             02/03/2009 - Added option to cancel script.
'**************************************************************************************************
 
'*****Search Computers container of AD for any machines with a name which begins "yourcompany"
'*****Returns name and Operating System information.
 
Set objConnection = CreateObject("ADODB.Connection")
objConnection.Open "Provider=ADsDSOObject;"
Set objCommand = CreateObject("ADODB.Command")
objCommand.ActiveConnection = objConnection
objCommand.CommandText = "<LDAP://cn=computers,dc=contoso,dc=com>;(&(objectCategory=computer)(cn=yourcompany*))" & ";name,operatingsystem;subtree"
Set objRecordSet = objCommand.Execute
 
'**************************************************************************************************
 
'*****Create array from yourcompany machines in Computers container. Then iterate through array, for each
'*****item prompt user to decide if machine should be moved. If answer is yes then machine is moved
'*****to correct OU.
 
Dim arrComputers : arrComputers = Array()
Dim intElem : intElem = 0
Set objShell = WScript.CreateObject("WScript.Shell")
Const TIMEOUT = 7
While Not objRecordSet.EOF
   ReDim Preserve arrComputers(intElem)
   arrComputers(intElem) = objRecordSet.Fields("name")
 OSVal = objShell.Popup("Do you want to move machine " & arrComputers(intElem) & "?" ,TIMEOUT,"More Info?",vbYesNoCancel + vbQuestion)
  Select Case OSVal
   Case vbYes
    If objRecordset.Fields("OperatingSystem") = "Windows Vista Enterprise" Then
     Set objOU = GetObject("LDAP://ou=Vista,ou=Workstations,ou=yourcompany,dc=contoso,dc=com")
     ObjLoc = "LDAP://cn=" & arrComputers(intElem) & ",cn=Computers,dc=contoso,dc=com"
     objOU.MoveHere objLoc, vbNullString
     wscript.echo "Machine has been moved to the Vista OU"
    Else
     Set objOU = GetObject("LDAP://ou=Production,ou=Workstations,ou=yourcompany,dc=contoso,dc=com")
      ObjLoc = "LDAP://cn=" & arrComputers(intElem) & ",cn=Computers,dc=contoso,dc=com"
      objOU.MoveHere objLoc, vbNullString
      wscript.echo "Machine has been moved to the Production OU"
     End If
   Case vbNo
    wscript.echo "Machine has not been moved."
   Case VbCancel
    wscript.Echo "Script has been cancelled."
    Wscript.Quit  
   End Select
     intElem = intElem + 1
   objRecordSet.MoveNext
Wend
 
'**************************************************************************************************
Posted in Guides Tagged with: ,

Leave a Reply

Your email address will not be published. Required fields are marked *

*