Jump to content
Sign in to follow this  

check to see if a computer already exists in AD

Recommended Posts


Here is what I am trying to do.

A script will run after our sysprep image during the "runonce" phase of setup. The computer is imaged with a random computer name and joined to a workgroup. It will preform the following steps.

prompt for AD username

prompt for AD password

search AD to see if a GUID already exists for this computer

If GUID is found present a box saying "this computer was found in AD as %computername%, do you wish to keep this name?"

If yes, rename computer to matching AD computer name and join domain.

If no/or no computer GUID was found, prompt for what you want to call this computer then take that input and add to domain.

I have most of the code already, but the one thing I don't know how to do is search AD for a computer by GUID (or at all)

I have found this code from the legendary add to domain script but I'm not sure how to modify it to suit our purpose.

; Check and see if this computername is already present in AD
$computersCN = returnObjectDN($computername, "computer")
If $computersCN <> "0" Then
    $computersOU = returnFullOUfromDN($computersCN) 
    UpdateProgressText("Found this computer (" & $computername & ") in the " & _
                returnFriendlyName($computersOU) & " OU - This has been selected " & _
                "for you below.")
    GuiCtrlSetState(_ArraySearch($treeIDArray, $computersOU), $GUI_FOCUS)

The problem I think will be that the computer currently has a random generated computer name after it's imaged, so I'm not sure how to search AD to see if it used to exist. Lets say the computer used to be called "finance34" but has since been reimaged and now it is on a workgroup with a random computer name of "winnt-22947387". How would I search AD to see if the computer USED to be called something even though it isn't currently called finance34? I assume the best way would be to look for the GUID, but if someone has a better suggestion that would be great.

Share this post

Link to post
Share on other sites

For some reason, I don't think this is right. Depending on your sysprep, a new GUID is created during the mini-setup, without knowing what the machine USED to be called and manually entering that some where, I don't think you can search for it because the GUIDs should never match.

We just edit our sysprep.ini file either after imaging with the computer name we want, or just give it an invalid name in the sysprep.ini, then during the mini-setup it will ask you to name the machine saying it is invalid.

Also using the adfunctions.au3 might save you some time in playing with AD:


Then again I honestly may just be wrong, prehaps I'll read this again and try to think of another response later if this was of no help now.

"Human kind cannot gain anything without first giving something in return, to obtain; something of equal value must be lost."The Help File is truly your friend.

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