Join and rejoin a computer / Move a Computer from OUs within a domain in a command line

Dear all,

I need to do two action in command line :

1-Join a computer (Windows XP or 7) to a Domain or specific domain controller  with an account that has Admin right

2-Re-join a computer  (Windows XP or 7) to the Domain or specific domain controller in case the computer crashed and the same computer name is kept after masterization...

3- Move a computer from one OU within another OU.

It looks like the second one is tricky because there is actually a deletion of the computer object and a recreation of the object in the new OU ????

I no i can use Netdom and Powershell but i'm new to the business and i need example i can rely on by just replacing what needs to be replace in the syntax.

Thank Guys
AMATERASOUAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
Jason RybergConnect With a Mentor Technical Consultant IVCommented:
Are you looking to perform these actions remotely or from a DC?

If from a DC, here's some PowerShell giving you what you're looking for:

Import-Module ActiveDirectory

#ask for credentials to perform tasks
$cred = Get-Credential

#Join a computer to the domain 
New-ADComputer thisisatestcomputer -Credential $cred
#optionally, place the computer in a target OU
New-ADComputer thisisatestcomputer -path "OU=SERVERBUILDS,DC=contoso,DC=com" -Credential $cred

#Check computer membership, delete if necessary, and rejoin
Get-ADComputer thisisatestcomputer | Remove-ADComputer -Confirm:$false -Credential $cred

#Move computer object's OU
Get-ADComputer thisisatestcomputer | Move-ADObject -targetpath "CN=Computers,DC=contoso,DC=com" -Credential $cred

Open in new window

0
 
Andy MIT Systems ManagerCommented:
I'm not sure of the exact syntax but I believe these can be done through using the DS.. commands in a command prompt. The following is the Technet article about it with the commands listed:

http://technet.microsoft.com/en-us/library/cc778414%28v=ws.10%29.aspx

For the second option I guess you would just remove/delete the AD object first then re-add it in the same method you would use for a new computer.
0
 
AMATERASOUAuthor Commented:
Hello,

Thank you so much but i need to perform this action remotely.

What should i do if i need to perform this action remotely ?

Thanks for your help.
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
Jason RybergTechnical Consultant IVCommented:
You'll need to run Enable-PSRemoting on your DC initially.  In other words, remote to your DC, open an elevated PowerShell prompt, and type Enable-PSRemoting

You might also need to set your Execution Policy to a lower level on the box you'll be running the script from:
Set-ExecutionPolicy Unrestricted

Open in new window


Next, establish a remote connection to your DC:
$RemoteServer = "dc01"
$s = new-pssession -computer $RemoteServer
Invoke-Command -session $s -script { Import-Module ActiveDirectory }
Import-PSSession -session $s -module ActiveDirectory

Open in new window


Then, you'll be able to run the commands above from your remote PowerShell prompt.
0
 
AMATERASOUAuthor Commented:
Thank you

But then last question how do i put all that together ?
Lets assume  i need to run the script from a 7 workstation or 2008 R2 server
in order to add machines to the domain ?
I'm not good at scripting :-(
Thank you
0
 
Jason RybergTechnical Consultant IVCommented:
Are you going to interact with the script (i.e. sitting in front of the screen typing), or will this be part of some kind of automated function?
0
 
AMATERASOUAuthor Commented:
I will be interacting with the script.
i will enter the name of the workstation remotely from a server or workstation and from there the folowing task are automated:

1- Join the computer to the domain and the right OU

2- Rejoin computer to the domain

3- Move the computer from one OU to another OU (I think this can be another script)

NB: I don't know if i'm in the right place for this post though.

Thanks a lot for your help and guidance.
0
 
Jason RybergTechnical Consultant IVCommented:
See the attached script.  You will need to rename the extension to ps1.  You will need to ensure you have set your execution policy to unrestricted to get it to run on your workstation.
Computer-Tasks.txt
0
 
AMATERASOUAuthor Commented:
Thank you so much!

But i have an error message stating that connexion couldn't be made to the server.
i've set the Execution policy to Unrestricted on my workstation.
should i do the same on the DC ?
Should I import powershell AD module on the DC ?
I have the version 3 of Powershell on my Windows 7 workstation.
I've already imported powershell AD module.
The DC i've set inside the script is my logonserver, and i've put the DC fqdn.

Thank you very much
Error-Message-Join.jpg
0
 
Jason RybergTechnical Consultant IVCommented:
Did you run Enable-PSRemoting on your DC?  Can you do that an then paste any error messages?
0
 
AMATERASOUAuthor Commented:
Error_Task-2_3Hello

First: Yes i did execute Enable-PSRemoting on the DC see attached screenshot (EnablePSremoting.jpg)

Second: I don't know for what reason but i can't remotely execute the script from my Win 7 Worstation remotely.... see attached screenshot (Executing Remotly_From-My Win7_KOEnablePSremotingError_Task-1_2_3 Executing Remotly_From-My Win7_KO.jpg)

Third: the computer Object already existed in the domain and when i tried to rejoin or move to another OU i had errors..... see attached screenshot (Error_Task-1_2_3.jpg)

Fourth: I deleted the computer Object in AD and tried a Join. The join worked (Created the computer obbject in AD) But the Rejoin or Move to OU did not work.....see attached screenshot (Error_Task-2_3.jpg)

When the machine was in the OU computer i tried to logon the machine as a domain user but the machine was still not in the domain.

I thank the script just create the object in AD but doesn't physically  join the machine to the domain

I was thinking can we put the target path in the script so the machine doesn't go srtaight to the computer container in AD.

I have a Forest with 4 child domains can we put a syntax for DC of other domains this will avoid modifying the server name in the script??

But for real THANK YOU!!!
0
 
Jason RybergTechnical Consultant IVCommented:
Let's tackle the (hopefully) easiest problem first.  Your error on establishing a remote connection to the DC shows that you're access is being denied.  Are you sure you're logging in with domain admin credentials or something similar?
0
 
AMATERASOUAuthor Commented:
Hello,

You right when i log on with my admin cred i'm able to connect remotely to the DC.
but i had to log on to my workstation with my admin cred. i thougt by login with my domain user account and right click on Ps1 to run as an administrator would do the job.
but as i was loged on with my domain user account it never ask me for my admin credential. on the contrary of when i was logged on with my domain admin account.
i was ask for admin credential when i ran Ps1 as an administrator

Oh well this was easy IT WORKS!!! Thank you!!!
0
 
Seth SimmonsSr. Systems AdministratorCommented:
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0
All Courses

From novice to tech pro — start learning today.