Solved

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

Posted on 2014-03-19
7
2,012 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 
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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Here's a look at newsworthy articles and community happenings during the last month.
In this post we will be converting StringData saved within a text file into a hash table. This can be further used in a PowerShell script for replacing settings that are dynamic in nature from environment to environment.
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles to another domain controller. Log onto the new domain controller with a user account t…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

696 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