Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

PowerShell Script to Join Domain and Rename Computer?

Posted on 2014-03-06
7
Medium Priority
?
7,648 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 668 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 668 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 11

Assisted Solution

by:NetoMeter Screencasts
NetoMeter Screencasts earned 664 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Wouldn't it be nice if objects in Active Directory automatically moved into the correct Organizational Units? This is what AutoAD aims to do and as a plus, it automatically creates Sites, Subnets, and Organizational Units.
It’s time for spooky stories and consuming way too much sugar, including the many treats we’ve whipped for you in the world of tech. Check it out!
This tutorial will walk an individual through setting the global and backup job media overwrite and protection periods in Backup Exec 2012. Log onto the Backup Exec Central Administration Server. Examine the services. If all or most of them are stop…
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 …
Suggested Courses

604 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