Solved

PowerShell Script to Join Domain and Rename Computer?

Posted on 2014-03-06
7
7,274 Views
2 Endorsements
Last Modified: 2014-03-07
I've used my judicious imitation skills to get a script together (looking around online) for joining a laptop to the domain, and also renaming it, AT THE SAME TIME.
Meaning, 1 reboot would be required, instead of naming computer and rebooting, then joining domain and rebooting.

Windows 7 Embedded thin client with Powershell 2.0.

I think it's working, as I'm able to logon to the system with my domain credentials, I'm just not sure if this is a good way to do it.

Have a look at the code, let me know what your expert opinions are, and if you suspect any repercussions that could come about later on.

It's joining the system to the domain, then re-naming the system based on our naming convention with the asset tag we put in the bios.

Then we reboot once and expect it to work fine from then on...

$comp=gwmi win32_computersystem
#Prompts you to enter in domain credentials to add
$cred=get-credential
#WMI Query to set new computer name
gwmi Win32_SystemEnclosure | % { $newname = "ThinClient-" + $_.SMBIOSAssetTag }
$domain="ourdomain.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


More on"JoinDomainOrWorkgroup method of the Win32_ComputerSystem class": http://msdn.microsoft.com/en-us/library/aa392154(v=vs.85).aspx
2
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
7 Comments
 
LVL 8

Accepted Solution

by:
N-W earned 167 total points
ID: 39911183
Your script looks fine to me.

The only issue you may have is if someone accidentally enters a duplicate asset tag into the BIOS, the script will overwrite the existing computer in AD and cause issues for that computer.

Other than that, there's no reason why you can't use this script to achieve your goals.
0
 

Author Comment

by:garryshape
ID: 39911192
Ok cool thanks.

Interesting consideration on the duplicate computer names.

Thinking maybe I can add some type of check in the script to make sure it doesn't exist already
0
 
LVL 14

Assisted Solution

by:Justin Yeung
Justin Yeung earned 167 total points
ID: 39911637
add
$checkname = get-adcomputer $newname -erroraction silentlycontinue
if ($checkname -eq $null)
{
your script to join domain
}
else
{
write-host "The name is alreay in AD, use othername"
}
0
Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

 
LVL 11

Assisted Solution

by:NetoMeter Screencasts
NetoMeter Screencasts earned 166 total points
ID: 39911935
Just another idea - I am using the following two lines in PS:
Add-Computer -DomainName netometer.com -force
Rename-Computer -NewName FS-12R2 -Restart

Open in new window

0
 

Author Comment

by:garryshape
ID: 39912458
Hi NetoMeter, thanks for the follow-up. Should that command help keep the computer on the domain? Or would it remove the trust?
0
 
LVL 11

Expert Comment

by:NetoMeter Screencasts
ID: 39912519
If the machine is already a member of the domain and you suspect that there is a problem with the machine's AD Account, you can re-join the domain by specifying just the short, NetBIOS name (ex. netometer).
0
 

Author Closing Comment

by:garryshape
ID: 39913053
Thanks gentlemen for easing my uncertainty on this matter and for the additional input.
0

Featured Post

Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

Question has a verified solution.

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

Background Information Recently I have fixed file server permission issues for one of my client. The client has 1800 users and one Windows Server 2008 R2 domain joined file server with 12 TB of data, 250+ shared folders and the folder structure i…
Did you know that more than 4 billion data records have been recorded as lost or stolen since 2013? It was a staggering number brought to our attention during last week’s ManageEngine webinar, where attendees received a comprehensive look at the ma…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

738 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