Skip to content

Join a domain

Terminal window
[CmdletBinding()]
Param(
[Parameter(Mandatory=$true)]
[string]$Packageroot,
[Parameter(Mandatory=$true)]
[string]$AppName,
[Parameter(Mandatory=$true)]
[string]$AppRelease,
[Parameter(Mandatory=$true)]
[string]$LogFile,
[Parameter(Mandatory=$true)]
[string]$TempFolder,
[Parameter(Mandatory=$true)]
[string]$DllPath,
[Parameter(Mandatory=$false)]
[Object]$InputObject=$null
)
try {
[string]$global:username='internal\userjoin'
[string]$global:domain='internal.mydomain.dk'
[string]$global:aeskey=@(159,185,19,134,62,241,80,22,70,244,23,168,237,185,210,199,58,255,173,93,250,159,101,244,247,44,211,43,100,217,26,25)
[string]$global:encpass='25892d1116743f0423413b16050a5345MgB8AGYAWgAwAGoAdAB1AHoANQBzAHUANgAwAHMAOQBWAEgAYgBSAG4AbwBsAFEAPQA9AHwAMAA2AGUAOQBjADkAZAAyAGYANQBkAGUAMAA3ADEAMgA0ADQAMAAzAGYAYQBjAGYAOAA5AGUAOQBjAGEAMQBmAGUANwA3AGQANABmADQAYQAwADQAZgA4ADcAYQA5AGMAMAAxADUANQAwAGMAYQA5ADkAZABhAGIAZAAzAGUANAA='
[string]$global:ou='OU=Windows 10,OU=Workstations,DC=internal,DC=mydomain,DC=dk'
### Download package kit
[bool]$global:DownloadPackage = $false
##############################################
#load core PS lib - don't mess with this!
if ($InputObject){$pgkit=""}else{$pgkit="kit"}
Import-Module (Join-Path $Packageroot $pgkit "PSlib.psm1") -ErrorAction stop
#load Library dll
$cs=Add-PSDll
##############################################
#Begin
$cs.Job_Start("WS",$AppName,$AppRelease,$LogFile,"INSTALL")
$cs.Job_WriteLog("[Init]: Starting package: '" + $AppName + "' Release: '" + $AppRelease + "'")
if (!$cs.Sys_IsMinimumRequiredDiskspaceAvailable('c:', 100)) {
Exit-PSScript -exitcode 1 -exitmessage "Minimum required disk space not available"
}
if ($global:DownloadPackage -and $InputObject){Start-PSDownloadPackage}
$cs.Job_WriteLog("[Init]: `$PackageRoot:` '" + $Packageroot + "'")
$cs.Job_WriteLog("[Init]: `$AppName:` '" + $AppName + "'")
$cs.Job_WriteLog("[Init]: `$AppRelease:` '" + $AppRelease + "'")
$cs.Job_WriteLog("[Init]: `$LogFile:` '" + $LogFile + "'")
$cs.Job_WriteLog("[Init]: `$TempFolder:` '" + $TempFolder + "'")
$cs.Job_WriteLog("[Init]: `$DllPath:` '" + $DllPath + "'")
$cs.Job_WriteLog("[Init]: `$global:DownloadPackage`: '" + $global:DownloadPackage + "'")
$domainInfo=Get-CimInstance -Namespace root\cimv2 -Class Win32_ComputerSystem
if ($domainInfo.PartOfDomain -eq $false){
Import-Module Microsoft.PowerShell.Management -UseWindowsPowerShell -NoClobber -WarningAction:SilentlyContinue
$cred=New-Object System.Management.Automation.PsCredential $global:username,($global:encpass | ConvertTo-SecureString -key $global:aeskey)
if ($global:ou) {
add-computer -domainname $global:domain -credential $cred -oupath $global:ou}
else {
add-computer -domainname $global:domain -credential $cred}
}
else{
$cs.Job_WriteLog("Computer is already joined to domain: $($domainInfo.Domain)")
}
Exit-PSScript 0
}
catch {
$line = $_.InvocationInfo.ScriptLineNumber
$cs.Job_WriteLog("*****************","Something bad happend at line $($line): $($_.Exception.Message)")
Exit-PSScript $_.Exception.HResult
}