Solved

PowerShell Script to Join Domain and Rename Computer?

Posted on 2014-03-06
7
7,049 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
Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

 
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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Office 365 & Microsoft Azure 8 62
Can I exclude specific directory with icacle command? 3 26
Windows IPv6 DHCP server 8 37
Exchange 2007 Powershell Query 9 18
The recent Microsoft changes on update philosophy for Windows pre-10 and their impact on existing WSUS implementations.
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This tutorial will walk an individual through the steps necessary to enable the VMware\Hyper-V licensed feature of Backup Exec 2012. In addition, how to add a VMware server and configure a backup job. The first step is to acquire the necessary licen…
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…

867 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