Solved

PowerShell Script to Join Domain and Rename Computer?

Posted on 2014-03-06
7
7,017 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

"Migrate" an SMTP relay receive connector to a new server using info from an old server.
Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
This tutorial will walk an individual through configuring a drive on a Windows Server 2008 to perform shadow copies in order to quickly recover deleted files and folders. Click on Start and then select Computer to view the available drives on the se…
This tutorial will walk an individual through the process of configuring their Windows Server 2012 domain controller to synchronize its time with a trusted, external resource. Use Google, Bing, or other preferred search engine to locate trusted NTP …

757 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

17 Experts available now in Live!

Get 1:1 Help Now