Script to join Domain

Hi
I'm trying to find a script that will allow me to join about 30 PC to the domain with powershell or other methods.
I try two different power shell scripts and I get this error ps1 cannot be loaded because running scripts is disabled on this system.
I try to add on the beginning of the script the following line but that did not help. Set-ExecutionPolicy Unrestricted.
I also use Kaseya to push scripts and I found a few scripts on txt format but they need to be under XML format so I convert a txt file to a xml file online but that did not work eider.
Any suggestion how I can achieve that by pushing a script to all PC's.
The script can be push and execute it by Kaseya VSA as long as the script works.
LVL 2
infedonetworkAsked:
Who is Participating?
 
Shaun VermaakConnect With a Mentor Technical Specialist/DeveloperCommented:
VBS I wrote a decade ago
Option Explicit

On Error Resume Next

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

Dim objWMISvc
Dim colItems
Dim objItem
Dim strDomain
Dim strUser
Dim strPassword
Dim objNetwork
Dim strComputer
Dim objComputer
Dim objWMIService
Dim colOperatingSystems
Dim ObjOperatingSystem
Dim ReturnValue

Set objWMISvc = GetObject( "winmgmts:\\.\root\cimv2" )
Set colItems = objWMISvc.ExecQuery( "Select * from Win32_ComputerSystem", , 48 )
For Each objItem in colItems
    If objItem.PartOfDomain = True Then
        WScript.Quit
    End If
Next

strDomain = "DOMAIN.com"
strUser = "USERNAME"
strPassword = "Password1"
 
Set objNetwork = CreateObject("WScript.Network")
strComputer = objNetwork.ComputerName
Set objNetwork = Nothing

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)

If ReturnValue = 0 Then
    Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate,(Shutdown)}!\\.\root\cimv2")
    Set colOperatingSystems = objWMIService.ExecQuery("Select * from Win32_OperatingSystem")
    For Each objOperatingSystem in colOperatingSystems
        ObjOperatingSystem.Reboot()
    Next
End If

Open in new window

0
 
Jose Gabriel Ortega CCEO J0rt3g4 Consulting ServicesCommented:
Ok, the errors are relative to the "Execution policy".
You can avoid that in multiple ways.

Here are 15 solutions:
Check the section:"Bypassing the PowerShell Execution Policy"

https://blog.netspi.com/15-ways-to-bypass-the-powershell-execution-policy/

And here's a script to test:
https://gallery.technet.microsoft.com/scriptcenter/Join-a-computer-to-a-domain-6e19d905
0
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
Setting the execution policy needs to be done outside of the script - if script execution is inhiited, the script cannot execute ;-).
You can run a PS script with  PowerShell -ExecutionPolicy Bypass -File 'C:\temp\script.ps1'
That should work with UNC paths too.
1
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
infedonetworkAuthor Commented:
You are the man.
I just join a 2016 Server to the Domain with no problem by using Kaseya to push the VB script to the local machine that need to be join then execute the VB script via Kaseya. Once I put all the agents on all the PC I will try with a mix of windows 7 and 10 and I'm sure
I will join all the PC's in less than 2 minutes at the same time.
I will let you know how it work before the end of the week but it should be good
0
 
infedonetworkAuthor Commented:
Any Chance you have one that copy profiles? I did not even look at that situation yet but once I join all the pc's to the Domain I will have to copy all their files from the old profile to the new one. The easy but not so easy when is 30+ users is to go on the registry and rename the profile key of the new logon user of the new domain to the old key of the old profile but that involve the users first to logon and then me to go one by one on the registry to rename it since they are all different. I will look in to that very soon. I toght you may have something that will make it easy. Thank you for your help. It saved me few hours.
0
 
Shaun VermaakTechnical Specialist/DeveloperCommented:
0
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
Note that ProfWiz free does not allow automation, and so it is a manual process. The paid version does.
Both can change the domain and take over the profile in one go.
0
 
infedonetworkAuthor Commented:
I know that. I already talk to them.
The Pro version is licence per tech and can be use more than once but even that does not allow automation. Has to be done one PC at the time.
The enterprise version allow all that but is 50 licences minimum and it can be use only for one project.
I'm looking at less expansive options for now. Is not that easy to get extra budget approved once it was already approved by the management.
I will keep looking if not I will do it via the registry one by one.
0
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
The only pro of the manual registry approach is that you can do it remotely. I would just run ProfWiz on each machine, one by one. Remember that if you just remap the profile path via registry you also should change security (ACLs), as those are set to the original (local or other domain) profile owner.
0
 
Jose Gabriel Ortega CCEO J0rt3g4 Consulting ServicesCommented:
Answered
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.