Solved

Rename and Join Domain script - problem when name exist in AD

Posted on 2014-03-19
7
1,690 Views
Last Modified: 2014-04-03
This is a script I am using after imaging a thin client laptop.
What it does is join the domain, then rename the laptop while joined to the domain according to our naming convention, resulting in the Domain-Joining and Computer-Naming step needing only 1 reboot.

The problem is, if the computer name already exist in AD, it will only rename the computer and it will not join the domain.

I've pieced together this script with the help of others (judicious imitation), however, I feel the part where I need it to join the domain even if the computer already exists requires more advanced knowledge of WMI/win32 with respect to PowerShell and AD.


#Name computer based on naming convention and SMBIOSAssetTag, then Join the Domain, Then Reboot

$comp=gwmi win32_computersystem -Authentication 6
#Prompts you to enter in domain credentials to add
$cred=get-credential
#WMI Query to set new computer name
gwmi Win32_SystemEnclosure | % { $newname = "CITY-" + $_.SMBIOSAssetTag }
$domain="domain.com"
#Join The Domain
$comp.JoinDomainOrWorkGroup($domain,$cred.getnetworkcredential().password,$cred.username,$null,3)
#Rename domain-joined Computer
$comp.rename($newname,$cred.getnetworkcredential().password,$cred.username)
Restart-Computer

Open in new window


This is the part I believe needs to be adjusted:
$comp.JoinDomainOrWorkGroup($domain,$cred.getnetworkcredential().password,$cred.username,$null,3)

Open in new window


The $null part is the OU, I believe, and the 3 part is a code.
The reference for that is here: http://msdn.microsoft.com/en-us/library/aa392154(v=vs.85).aspx
I believe "3" is the sum of "1" and "2":
1 being "Default. Joins a computer to a domain. If this value is not specified, the join is a computer to a workgroup."
2 being "Creates an account on a domain."

What I don't see here are any flags/bits that would let me delete the computer in AD before joining, or joining the system using the same name.

I tried 32 and 35 instead of 3 but that doesn't work.
0
Comment
Question by:garryshape
  • 4
  • 2
7 Comments
 
LVL 7

Accepted Solution

by:
BT15 earned 334 total points
ID: 39943109
http://msdn.microsoft.com/en-us/library/aa392154(v=vs.85).aspx

you might try using4 instead:

$comp.JoinDomainOrWorkGroup($domain,$cred.getnetworkcredential().password,$cred.username,$null,4) 

Open in new window


unless you have already tried this, you might want to rename the computer prior to joining the domain.
0
 

Author Comment

by:garryshape
ID: 39943309
Ok I will try. I wasn't certain on the wording of that.
So 4 means it deletes the object from AD before creating the object right?
0
 
LVL 7

Assisted Solution

by:BT15
BT15 earned 334 total points
ID: 39943341
not really sure. the wording confused me also.

I'll admit, using this method for joining machines to the domain is a little foreign to me.

if you had either the RSAT tools from Microsoft or the Quest AD tools installed on your image, I would suggest using the commandlets there to check for the existence of the name before attempting to join:

an example in RSAT:

if (get-computer $computername -credential $cred) {
   write-host $computername exists. removing
   remove-adcomputer $computername -credential $cred -confirm:$false
 }


#now use your code to rename and join
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 51

Assisted Solution

by:Netman66
Netman66 earned 166 total points
ID: 39943645
Why not use just 1?  if the account exists, it will use it then rename.  If the account doesn't exist it joins with existing name then renames it.

I see no logic to using 3.
0
 

Author Comment

by:garryshape
ID: 39944394
Sorry , I will try it with just 1 next time tomorrow and report back.
0
 

Author Comment

by:garryshape
ID: 39945505
1 doesn't appear to work.
It stays in the work group.

I have to delete the computer from Active Directory first then the script will work
0
 

Author Closing Comment

by:garryshape
ID: 39976078
Thanks for the input, the issue appears to be with our AD servers and an admin is looking into. Scripts solutions work.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Disabling the Directory Sync Service Account in Office 365 will stop directory synchronization from working.
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
This tutorial will walk an individual through the steps necessary to join and promote the first Windows Server 2012 domain controller into an Active Directory environment running on Windows Server 2008. Determine the location of the FSMO roles by lo…
This tutorial will walk an individual through the process of configuring their Windows Server 2012 domain controller to synchronize its time with a trusted, external resource. Use Google, Bing, or other preferred search engine to locate trusted NTP …

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now