+ Post New Thread
Results 1 to 4 of 4
O/S Deployment Thread, Script to remove old computer name from AD in Technical; Hey guys and gurls, We currently use FOG to image our machines with a Windows 7 Syspreped image on a ...
  1. #1

    Join Date
    Nov 2010
    Location
    Maidenhead
    Posts
    44
    Thank Post
    1
    Thanked 9 Times in 9 Posts
    Rep Power
    9

    Question Script to remove old computer name from AD

    Hey guys and gurls,

    We currently use FOG to image our machines with a Windows 7 Syspreped image on a windows domain on a windows server 2008 R2 dc.

    I have written scripts to install drivers, check for updates, add to active directory,etc so the build can be deployed without user interaction

    However, I would like to add a feature into this that will remove the computer with the same name on the domain. As we sometimes have to reimage a computer(s) that are already on domain, I would like a script that if it fails to add to domain using my scripts puts a txt file with the name of the PC itself onto a share/server. Then this server which is on the domain checks with a script every 5-15mins and if it sees a txt file gets its/there names and searches AD and removes the computers in question. Then the new computer will wait 5-15 mins and the run the task again, therefore it should be able to add itself to domain.

    I can do the new computer side of scripts but need help getting a script on the server to check for the text file and then remove the associated computer from domain based on this information.

    I thought there might be a way of on the new computer as to over rule the existing computer object.

    Thanks in advance for your guys help,

    Tom

  2. #2

    Michael's Avatar
    Join Date
    Dec 2005
    Location
    Birmingham
    Posts
    9,262
    Thank Post
    242
    Thanked 1,568 Times in 1,250 Posts
    Rep Power
    340
    By default, when a computer leaves the domain in a 2008 R2 environment, the Computer Object remains but is disabled.

    If you then re-joined a computer (using the same name), it would automatically re-use the existing Computer Object.

    The same applies if you re-imaged a workstation without demoting it from the domain in the first instance.

    You only need to delete Computer Objects if you know it'll never be used again or if you changed your workstation naming structure (for example).

  3. Thanks to Michael from:

    forkies (19th June 2012)

  4. #3

    Join Date
    Nov 2010
    Location
    Maidenhead
    Posts
    44
    Thank Post
    1
    Thanked 9 Times in 9 Posts
    Rep Power
    9
    Well thats what I thought at first, however if I leave the computer in AD and try to add the new computers the script fails... I have set it to check to see if it is part of domain and if not try again but that still fails the second time on all computers.

    Heres my script which does work to add computers to domain automatically when they are not on already.
    Const JOIN_DOMAIN = 1
    Const ACCT_CREATE = 2
    Const ACCT_DELETE = 4
    Const WIN9X_UPGRADE = 16
    Const DOMAIN_JOIN_IF_JOINED = 32
    Const JOIN_UNSECURE = 64
    Const MACHINE_PASSWORD_PASSED = 128
    Const DEFERRED_SPN_SET = 256
    Const INSTALL_INVOCATION = 262144
    Dim message
    Dim IP


    strDomain = "domain.local"
    strUser = "user"
    strPassword = "password"

    Set objNetwork = CreateObject("WScript.Network")
    strComputer = objNetwork.ComputerName

    Set objWMIService = GetObject ("winmgmts:" & "!\\" & strComputer & "\root\cimv2")
    Set colAdapters = objWMIService.ExecQuery ("Select * from Win32_NetworkAdapterConfiguration Where IPEnabled = True")

    Dim objNTInfo
    Set objNTInfo = CreateObject("WinNTSystemInfo")

    GetComputerName = lcase(objNTInfo.ComputerName)

    newComputerName = GetComputerName

    If newComputerName = "" Then
    Wscript.quit
    End If

    Set objComputer = GetObject("winmgmts:{impersonationLevel=Impersonat e}!\\" & _
    strComputer & "\root\cimv2:Win32_ComputerSystem.Name='" & _
    strComputer & "'")

    ReturnValue = objComputer.JoinDomainOrWorkGroup(strDomain, strPassword, strDomain & "\" & strUser, "OU=ICT1,OU=Curric PC's,OU=school computers,DC=domain,DC=local", JOIN_DOMAIN + ACCT_CREATE)
    I would apprieciate any help with a fix to an issue that could cause this not to work.

    Tom

  5. #4

    Join Date
    Nov 2010
    Location
    Maidenhead
    Posts
    44
    Thank Post
    1
    Thanked 9 Times in 9 Posts
    Rep Power
    9
    Well I have reimaged an ICT suite last night and they are all on domain this morning even though I didn't remove from domain first. Seems you were right

    I had edited the scripts to check if on domain and if not run the script one more time to try adding it again to domain, don't know if this helped or not.

    Thanks,
    Tom

SHARE:
+ Post New Thread

Similar Threads

  1. Script to remove users from one group and add to another
    By JMBates in forum Windows Server 2000/2003
    Replies: 5
    Last Post: 11th April 2012, 10:02 AM
  2. Replies: 5
    Last Post: 11th April 2012, 10:02 AM
  3. Replies: 13
    Last Post: 22nd April 2011, 03:50 PM
  4. Replies: 1
    Last Post: 6th December 2007, 09:20 AM
  5. Script to shut down computers in a certain OU
    By tosca925 in forum Scripts
    Replies: 11
    Last Post: 25th September 2005, 09:23 PM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •