Script to add machine to domain then put in correct OU

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
Thomas NSystems Analyst - Windows System AdministratorAsked:
Who is Participating?
 
Felix LevenConnect With a Mentor Senior System and DatabaseadministratorCommented:
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
 
Felix LevenSenior System and DatabaseadministratorCommented:
Powershell:
Add-Computer -domainname Domain02 -OUPath OU=testOU,DC=domain,DC=Domain,DC=com

Open in new window

0
 
jss1199Commented:
netdom join %COMPUTERNAME% /domain:txieelabs.local /ou:OU=thomas,DC=txieelabs,DC=local /userd:txieelabs\administrator /passwordd:*** /reboot

Open in new window

0
Simplify Active Directory Administration

Administration of Active Directory does not have to be hard.  Too often what should be a simple task is made more difficult than it needs to be.The solution?  Hyena from SystemTools Software.  With ease-of-use as well as powerful importing and bulk updating capabilities.

 
Felix LevenSenior System and DatabaseadministratorCommented:
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
 
Thomas NSystems Analyst - Windows System AdministratorAuthor Commented:
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
 
Thomas NSystems Analyst - Windows System AdministratorAuthor Commented:
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
 
jss1199Commented:
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
 
Thomas NSystems Analyst - Windows System AdministratorAuthor Commented:
Also I am using Windows 7. Will this work with this OS?
0
 
jss1199Commented:
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
 
Thomas NSystems Analyst - Windows System AdministratorAuthor Commented:
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
 
jss1199Commented:
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
 
Thomas NSystems Analyst - Windows System AdministratorAuthor Commented:
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
 
Thomas NSystems Analyst - Windows System AdministratorAuthor Commented:
I just want to have a .cmd file that I click then it adds machine to domain and moves it to correct OU.
0
 
Felix LevenSenior System and DatabaseadministratorCommented:
powshell is the way to go the next years, file must be ps1
0
 
Thomas NSystems Analyst - Windows System AdministratorAuthor Commented:
Thanks MrGraves,
   I try to run it but nothing happens. Any suggestions?
0
 
Thomas NSystems Analyst - Windows System AdministratorAuthor Commented:
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
 
Felix LevenSenior System and DatabaseadministratorCommented:
Create a shortcut with "Target":
powershell.exe -command "& 'C:\MyScript.ps1'"
0
 
Thomas NSystems Analyst - Windows System AdministratorAuthor Commented:
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
 
Felix LevenSenior System and DatabaseadministratorCommented:
open powershell first and test the script -> working ?

if you need to change the execution policy for scripts -> Set-ExecutionPolicy RemoteSigned
0
 
Felix LevenSenior System and DatabaseadministratorCommented:
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
 
Thomas NSystems Analyst - Windows System AdministratorAuthor Commented:
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
 
Felix LevenSenior System and DatabaseadministratorCommented:
See my last comment, no file needed just a shortcut
0
 
Felix LevenSenior System and DatabaseadministratorCommented:
.\myscript.ps1

to start the script on the shell
0
 
Thomas NSystems Analyst - Windows System AdministratorAuthor Commented:
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
 
Felix LevenSenior System and DatabaseadministratorCommented:
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
 
Thomas NSystems Analyst - Windows System AdministratorAuthor Commented:
Sorry, im new to power shell and scripting..so excuse my ignorance.

How and where do I put "set-executionpolicy remotesigned"?
0
 
Felix LevenSenior System and DatabaseadministratorCommented:
open powershell and paste the command, but

test the shortcut first please
0
 
Thomas NSystems Analyst - Windows System AdministratorAuthor Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.