Adding machine to Correct OU during Auto Join Domain .VBS
This is for WIndows 8.1 I can now get it to run this VBS and it adds the machine to the domain correctly, however if I remove NULL and place strOU, and place the Full AD line it does NOT work at all.... Can anyone help out? Thanks
Const JOIN_DOMAIN = 1
Const ACCT_CREATE = 2
Const ACCT_DELETE = 4
Const WIN9X_UPGRADE = 16
Const DOMAIN_JOIN_IF_JOINED = 32
Const JOIN_UNSECURE = 64
Const MACHINE_PASSWORD_PASSED = 128
Const DEFERRED_SPN_SET = 256
Const INSTALL_INVOCATION = 262144
strDomain = "xxxxx"
strPassword = "xxx"
strUser = "xxxx"
strOU = "OU=_General Users,OU=Computers,OU=Off-site,DC=xxx,DC=xxx,DC=xxxx,DC=xxx"
Set objNetwork = CreateObject("WScript.Network")
strComputer = objNetwork.ComputerName
Set objComputer = GetObject("winmgmts:{impersonationLevel=Impersonate}!\\" & _
strComputer & "\root\cimv2:Win32_ComputerSystem.Name='" & _
strComputer & "'")
ReturnValue = objComputer.JoinDomainOrWorkGroup(strDomain, _
strPassword, strDomain & "\" & strUser, NULL, _
JOIN_DOMAIN + ACCT_CREATE)
Again, If I remove NULL above, and place strOU it will not join the domain at all.. If I LEAVE NULL in its place its ignores the OU string and adds the domain correctly, just places it in the default OU in AD... Thank you for any help
Answers (2)
Use the answer file using a syspreped image.You can use ADSI edit to find the path of the OU you want.
<Identification>
<Credentials>
<Domain>Oglesby.local</Domain>
<Password>DomainPassword</Password>
<Username>DomainUserWithRightsToAddComputerAcct</Username>
</Credentials>
<JoinDomain>Oglesby.local</JoinDomain>
<MachineObjectOU>OU=VDI,DC=oglesby,DC=local </MachineObjectOU>
<UnsecureJoin>False</UnsecureJoin>
</Identification>
</component>
Comments:
-
Answer file should work, but there may be a reason the OP is using a script instead. At my company, we like to make sure our antivirus and Windows Updates have been applied before we join a system to the domain. It's also possible if his permissions are not correct that using the answer file would fail as well. - BHC-Austin 10 years ago
If youre deploying 8.1 then you should really stop using VB-script IMHO, both for computer renaming and domain joining.
Why? VB script is uggly (again IMHO), and hard to use (let the flaming begin :-) ). Also WSName is not supported nor developed any more.
PowerShell is the way to go, super easy, super clean and there’s an abundance of info on google for this.
Computer rename (Use K2 3.6 and tick that this requires a reboot in the post install task):
Bat file:
powershell.exe -nologo -executionpolicy bypass -noprofile -file ".\ComputerRename.ps1"
ComputerRename.ps1, (I'm pulling the servicetag and put it in the computername, which I find useful, edit as you like) (Google it and you'll find infinite ideas on this, WMI is your friend):
$serial=Get-WMIObject -Class Win32_Bios | select -expand "SerialNumber"
$NewName="ws-"+ $serial
$ComputerInfo = Get-WmiObject -Class Win32_ComputerSystem
$ComputerInfo.Rename($NewName)
Zip those and run the bat in a post deployment task.
Domain Join (Again, use K2 3.6 and tick that this requires a reboot in the post install task)
I use ksleep.exe to let everything settle down after the reboot. Had some issues on sites with no DC locally prior to using that, but it can surley be omitted in ideal conditions.
Bat file:
ksleep.exe 10
powershell.exe -nologo -executionpolicy bypass -noprofile -file ".\jd.ps1"
jd.ps1:
$domain = "domainname"
$password = "pwd for a user account that can add computers to domains" | ConvertTo-SecureString -asPlainText -Force
$username = "$domain\accountname"
$credential = New-Object System.Management.Automation.PSCredential($username,$password)
$ouPath="OU=Computers,OU=My OU,DC=my,DC=domain"
add-computer -Credential $credential -DomainName $domain -OUPath $ouPath
Zip those and run the bat in a post deployment task.
Lastly as someone surely will point out looking at the documentation for "add-computer" actually it seems that it supports renaming and joining in one swoop. I haven't got that working in win 7 after upgrading to the latest version of the management tool, but it might work in 8/8.1 .
Also, everything in this post is stolen from others, so any credit is to everyone else... :-)
I know that using the answer file is MS's method, I was just providing some justification for using script vs the answer file for future readers as I'm sure there are many reasons (beyond the examples I provided) why you might want to do this outside of the answer file. - BHC-Austin 10 years ago