Jump to content

Recommended Posts

Hi All,

I'm trying to understand what I need to do you get a script that will join a computer to my AD. Ideally I'd like to run the script and the PC joins the domain and is restarted.

I had tested a command in PowerShell (Run as Admin) that will join a computer to the domain, however I am prompted for admin credentials. When I try and run (what I believe!) to be the same command through a script it just says the command fails.

Here is what I have:

Console Shell: net use on %consolenumber%

SET: @OutputOfNetUse@ = %Shellresults%

Console Shell: c:\windows\system32\windowspowershell\v.1.0\powershell.exe -noprofile -executionpolicy bypass -command "add-computer –domainname mydomain.org -Credential(Get-Credential)"

Console Shell: c:\windows\system32\windowspowershell\v1.0\powershell.exe -noprofile -executionpolicy bypass -command "Restart-Computer"

I think it would make sense if the credentials were actually the stored credentials within the location of LabTech. If anyone can point me in the right direction it would be greatly appreciated!

Kind regards,

 

Share this post


Link to post
Share on other sites
Posted (edited)

The script we put together has two steps, and makes a couple assumptions.

We have @DOMAINNAME@ set as a location EDF

Script Step 1 - ExtraData Get Value --- Set variable @DOMAINNAME@ 

Script Step 2 - WRITE TEXT FILE   --- JoinDomain.ps1

$user = "@DOMAINNAME@\%ComputerUsername%"
$pass = ConvertTo-SecureString "%ComputerPassword%" -AsPlainText -Force 
$DomainCred = New-Object System.Management.Automation.PSCredential $user, $pass 
Rename-Computer "@NewName@"
Add-Computer -DomainName @DOMAINNAME@ -NewName "@NewName@" -credential $DomainCred 

The %computername% and %computerpassword% are then pulled from the location's automate credentials. The Rename-Computer command and likewise the -NewName parameter is optional, our domain join script is really part of our Deployment Process, so we have a parameter in the script the technician enters the NewName while running it.

Script Step 3 - Shell Command

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe  -ExecutionPolicy Bypass -NoLogo -NonInteractive  -command "PATHGOESHERE\JoinDomain.ps1"

SCript Step 4 - File Delete ---- JoinDomain.ps1 (because you don't really want that hanging around) 

Edited by JosephM

Share this post


Link to post
Share on other sites

Hi JosephM

Thank you for the reply!

Script Step 1:

Under System Dashboard - Config - Configurations - Additional Fields - I added DOMAINNAME. Field Type: Text. Is that what you mean?

Then the statement is: GET: @myDomain@ = [EXTRAFIELD DOMAINNAME]

Script Step 2:

For testing purposes I have created JoinDomain.ps1 in the packages folder on the computer (I'll copy the file down once I have this working). I also will add renaming the PC hostname later. So my second statement is:

Console Shell: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe  -ExecutionPolicy Bypass -NoLogo -NonInteractive  -command "C:\Windows\LTSvc\packages\JoinDomain.ps1"

The script then fails with this message: The reason: ScriptFunctionExtraDataGetValue failed processing query. So I am guessing it is to do with the extra field. In the statement:

Extra Field: DOMAINNAME-1

ID: @DOMAINNAME@

Variable: myDomain

 

....where I have written "myDomain". Any more pointers?!

 

Kind regards,

 

 

 

Share this post


Link to post
Share on other sites

I don't see anyone mentioning that anything "as admin" is a bad idea... There are various reasons come to slack and ask me why 😏

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...