Solved

PowerShell Script to Join Domain and Rename Computer?

Posted on 2014-03-06
7
7,228 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
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
Free eBook: Backup on AWS

Everything you need to know about backup and disaster recovery with AWS, for FREE!

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
Windows 10 came with  a lot of built in applications, Some organisations leave them there, some will control them using GPO's. This Article is useful for those who do not want to have any applications in their image (example:me).
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 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…

740 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