scripts/MDT-JoinDomain.ps1
2025-04-09 23:17:45 -05:00

71 lines
2.7 KiB
PowerShell

# Script to join domain inside of MDT.
# Technolog Networks, 2024-08-12
# iRaven
# General necessities.
$CompName = $env:COMPUTERNAME
$TSEnv = New-Object -ComObject Microsoft.SMS.TSEnvironment
$MDTPath = $Global:TSEnv.Value("DeployRoot")
Start-Transcript -Path "$MDTPath\_Logs\$CompName-JoinDomain.log" -Append
$CompType = $args[0]
$SubType = $args[1]
if ($CompType -like "WKS") {
New-ItemProperty -Path "HKLM:\System\CurrentControlSet\Control\LSA" -Name NetJoinLegacyAccountReuse -Value 1
$PCNamePrefix = ($CompName.Substring(0,$CompName.IndexOf("-")))
# Domain credentials to join WKS to domain
. "$MDTPath\_Scripts\Credentials-MDTJoinDomain.ps1"
Write-Host "Auto-detecting computer name $CompName..."
# Filter out OUs
if ($PCNamePrefix -like "XH") {
Write-Host "Detected computer name as XH - domain joining to Personal WKS OU"
$DomainOU = "OU=Personal,OU=Workstations,DC=technolog,DC=net"
}
elseif ($PCNamePrefix -like "TNG" -or $PCNamePrefix -like "NH" -or $PCNamePrefix -like "IR") {
Write-Host "Detected computer name as TNG, NH, or IR - domain joining to Standard WKS OU"
$DomainOU = "OU=Generic,OU=Workstations,DC=technolog,DC=net"
}
elseif ($PCNamePrefix -like "TEST") {
Write-Host "Detected computer name as TEST - domain joining to Testing WKS OU"
$DomainOU = "OU=TestingWKS,OU=Workstations,DC=technolog,DC=net"
}
else {
Write-Host "Computer name is unusual - domain joining to Standard WKS OU"
$DomainOU = "OU=Workstations,DC=technolog,DC=net"
}
} elseif ($CompType -like "Server") {
$DomainCredential = Get-Credential -Message "To join this server to the domain, enter your domain credentials."
# Filter out SubType var for server tiers
if ($null -ne $SubType) {
$DomainOU = "OU=$SubType,OU=Domain Servers,DC=technolog,DC=net"
} else {
$DomainOU = "OU=Domain Servers,DC=technolog,DC=net"
}
} elseif ($CompType -like "Utility") {
. "$MDTPath\_Scripts\Credentials-MDTJoinDomain.ps1"
Write-Host "Domain joining to Utility OU"
$DomainOU = "OU=Utility,DC=technolog,DC=net"
} else {
. "$MDTPath\_Scripts\Credentials-MDTJoinDomain.ps1"
Write-Host "Domain joining to generic Computers OU"
}
if ([boolean](get-variable "DomainOU" -ErrorAction SilentlyContinue)) {
try {
Add-Computer -DomainName technolog.net -Credential $DomainCredential -OUPath $DomainOU -Restart
} catch {
Write-Host "Couldn't join the domain with OU $DomainOU"
}
}
else {
try {
Add-Computer -DomainName technolog.net -Credential $DomainCredential -Restart
} catch {
Write-Host "Couldn't join the domain!"
}
}
Stop-Transcript