Solved

Script to add machine to domain then put in correct OU

Posted on 2013-12-09
28
1,469 Views
Last Modified: 2013-12-18
I need a script that will put a machine on the domain and then add the machine to the correct OU. We used to use sysprep but we cant use that anymore. Can anybody assist with this? Thanks
0
Comment
Question by:Thomas N
[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
  • 13
  • 11
  • 4
28 Comments
 
LVL 13

Expert Comment

by:Felix Leven
ID: 39706429
Powershell:
Add-Computer -domainname Domain02 -OUPath OU=testOU,DC=domain,DC=Domain,DC=com

Open in new window

0
 
LVL 19

Expert Comment

by:jss1199
ID: 39706430
netdom join %COMPUTERNAME% /domain:txieelabs.local /ou:OU=thomas,DC=txieelabs,DC=local /userd:txieelabs\administrator /passwordd:*** /reboot

Open in new window

0
 
LVL 13

Expert Comment

by:Felix Leven
ID: 39706451
add-computer also can do the restart and pass credentials:

Add-Computer -credentials domain\user -domainname Domain02 -OUPath OU=testOU,DC=domain,DC=Domain,DC=com -restart

Open in new window

0
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 

Author Comment

by:Thomas N
ID: 39706496
Thanks jss1199,
  So ill just save it as a .vbs script and test it. So ill need to download netdom and I can put it in say a USB drive with the script and run it on any machine that needs it, correct?


A couple questions:

-"/ou:OU=thomas" is the OU that the computer will go in correct? What if its in a OU underneath the thomas OU?

-"%COMPUTERNAME%" Do I need to put a computer name in here or does it auto populate a name for me?
 
Thanks
0
 

Author Comment

by:Thomas N
ID: 39706497
Thanks jss1199,
  So ill just save it as a .vbs script and test it. So ill need to download netdom and I can put it in say a USB drive with the script and run it on any machine that needs it, correct?


A couple questions:

-"/ou:OU=thomas" is the OU that the computer will go in correct? What if its in a OU underneath the thomas OU?

-"%COMPUTERNAME%" Do I need to put a computer name in here or does it auto populate a name for me?
 
Thanks
0
 
LVL 19

Expert Comment

by:jss1199
ID: 39706517
you can save as cmd file unless you have other script portions that are vbs.  

If you need to add more depth to the OU, just add an additional OU statement, such as OU=level1, OU=level2,OU=thomas

%ComputerName% is a system variable that passes the name of the local machine you are running the command on.  If you need to rename the computer before joining to the domain, you can use:

netdom renamecomputer %Computername% /newname:newcomputername
0
 

Author Comment

by:Thomas N
ID: 39706563
Also I am using Windows 7. Will this work with this OS?
0
 
LVL 19

Expert Comment

by:jss1199
ID: 39706626
yes.

You can also use powershell, which is installed by default on Win7, similar to the below:

Add-Computer -DomainName "MY.DOMAIN.COM" -OUPath ("OU=Computers,DC=MY,DC=DOMAIN,DC=COM")
0
 

Author Comment

by:Thomas N
ID: 39706702
Is this right?

Add-Computer -credentials (domain\last.name password) -domainname (domain.net) -OUPath (OU=computers,DC=domain,DC=net) -restart


Im in a single domain forest.
0
 
LVL 19

Expert Comment

by:jss1199
ID: 39706869
Should work, but I typically move the credential further back, such as:

Add-Computer  -domainname (domain.net) -OUPath (OU=computers,DC=domain,DC=net) -credentials (domain\last.name password) -restart
0
 

Author Comment

by:Thomas N
ID: 39707109
It didnt work. When I click on the cmd file it just popped up and went away. I put a pause in there and it tells me "add-computer is not a recognized command".

Am I suppose to put in the path to the powershell folder? Ex:C:\powershell\add-computer)?
0
 

Author Comment

by:Thomas N
ID: 39707153
I just want to have a .cmd file that I click then it adds machine to domain and moves it to correct OU.
0
 
LVL 13

Expert Comment

by:Felix Leven
ID: 39707912
powshell is the way to go the next years, file must be ps1
0
 

Author Comment

by:Thomas N
ID: 39709086
Thanks MrGraves,
   I try to run it but nothing happens. Any suggestions?
0
 

Author Comment

by:Thomas N
ID: 39709097
When I click on it to use powershell, it comes up and goes away quickly. I put a pause in it to try and stop it to see the error but it didnt work.
0
 
LVL 13

Expert Comment

by:Felix Leven
ID: 39709148
Create a shortcut with "Target":
powershell.exe -command "& 'C:\MyScript.ps1'"
0
 

Author Comment

by:Thomas N
ID: 39709453
Im not sure whats going on. Still the same thing. I moved the script to the C drive and renamed it myscript.ps1. I then created a shortcut using target with the line you put and it still just pops up a window then goes away.
0
 
LVL 13

Expert Comment

by:Felix Leven
ID: 39710741
open powershell first and test the script -> working ?

if you need to change the execution policy for scripts -> Set-ExecutionPolicy RemoteSigned
0
 
LVL 13

Expert Comment

by:Felix Leven
ID: 39711368
Shortcut with target:

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -noprofile -noexit -command "& {Add-Computer -credentials domain\user -domainname yourdomain -OUPath OU=testOU,DC=domain,DC=Domain,DC=com -restart}"
0
 

Author Comment

by:Thomas N
ID: 39711732
This is what I get when I run the "myscript.ps1"

PS C:\> myscript.ps1
The term 'myscript.ps1' is not recognized as the name of a cmdlet, function, script file, or operable program. Check th
e spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:13
+ myscript.ps1 <<<<
    + CategoryInfo          : ObjectNotFound: (myscript.ps1:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException


Suggestion [3,General]: The command myscript.ps1 was not found, but does exist in the current location. Windows PowerShe
ll doesn't load commands from the current location by default. If you trust this command, instead type ".\myscript.ps1".
 See "get-help about_Command_Precedence" for more details.
PS C:\>
0
 
LVL 13

Expert Comment

by:Felix Leven
ID: 39711747
See my last comment, no file needed just a shortcut
0
 
LVL 13

Expert Comment

by:Felix Leven
ID: 39711792
.\myscript.ps1

to start the script on the shell
0
 

Author Comment

by:Thomas N
ID: 39712303
Okay, this is what I get now.

File C:\myscript.ps1 cannot be loaded because the execution of scripts is disabled on this system. Please see "get-help
 about_signing" for more details.
At line:1 char:15
+ .\myscript.ps1 <<<<
    + CategoryInfo          : NotSpecified: (:) [], PSSecurityException
    + FullyQualifiedErrorId : RuntimeException
0
 
LVL 13

Expert Comment

by:Felix Leven
ID: 39713334
See my comment before:

Set-ExecutionPolicy RemoteSigned

to allow local scripts to be executed.

then make the shortcut with target:

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -noprofile -noexit -command "& {Add-Computer -credentials domain\user -domainname yourdomain -OUPath OU=testOU,DC=domain,DC=Domain,DC=com -restart}"
0
 

Author Comment

by:Thomas N
ID: 39721698
Sorry, im new to power shell and scripting..so excuse my ignorance.

How and where do I put "set-executionpolicy remotesigned"?
0
 
LVL 13

Expert Comment

by:Felix Leven
ID: 39721714
open powershell and paste the command, but

test the shortcut first please
0
 

Author Comment

by:Thomas N
ID: 39722263
I ran it manually from a powershell window. Here is what I get.



PS C:\Users\cs$ervice> set-executionpolicy remotesigned

Execution Policy Change
The execution policy helps protect you from scripts that you do not trust. Changing the execution policy might expose
you to the security risks described in the about_Execution_Policies help topic. Do you want to change the execution
policy?
[Y] Yes  [N] No  [S] Suspend  [?] Help (default is "Y"): y

PS C:\Users\cs$ervice> Add-Computer  -domainname domain.net -OUPath OU=computers,DC=domain,DC=net -credentials domain\user password44 -restart
Add-Computer : Cannot convert 'System.Object[]' to the type 'System.String' required by parameter 'OUPath'. Specified m
ethod is not supported.
At line:1 char:47
+ Add-Computer  -domainname domain.net -OUPath <<<<  OU=computers,DC=domain,DC=net -credentials domain\user password44 -restart
    + CategoryInfo          : InvalidArgument: (:) [Add-Computer], ParameterBindingException
    + FullyQualifiedErrorId : CannotConvertArgument,Microsoft.PowerShell.Commands.AddComputerCommand

PS C:\Users\cs$ervice>
0
 
LVL 13

Accepted Solution

by:
Felix Leven earned 500 total points
ID: 39723308
The exeution policy matter if you are executing scripits only.

Add-Computer  -domainname domain.net -OUPath "OU=computers,DC=domain,DC=net" -credentials domain\user -restart

it will ask for you password, if not necessary dont type your passwords in clear text anywhere
0

Featured Post

Free eBook: Backup on AWS

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

Question has a verified solution.

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

With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
A hard and fast method for reducing Active Directory Administrators members.
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

739 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