# 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] Function CheckDomainJoin { # Check if we're already domain joined if ((gwmi Win32_ComputerSystem).partofdomain -eq $true -and (gwmi Win32_ComputerSystem).domain -eq "technolog.net") { write-host "Already part of the domain!" return true } else { write-host "Not part of the domain yet (or joined to another)!" return false } } Function FilterComputerOU { 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" $Script.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" $Script.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" $Script.DomainOU = "OU=TestingWKS,OU=Workstations,DC=technolog,DC=net" } else { Write-Host "Computer name is unusual - domain joining to Standard WKS OU" $Script.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) { $Script.DomainOU = "OU=$SubType,OU=Domain Servers,DC=technolog,DC=net" } else { $Script.DomainOU = "OU=Domain Servers,DC=technolog,DC=net" } } elseif ($CompType -like "Utility") { . "$MDTPath\_Scripts\Credentials-MDTJoinDomain.ps1" Write-Host "Domain joining to Utility OU" $Script.DomainOU = "OU=Utility,OU=Workstations,DC=technolog,DC=net" } else { . "$MDTPath\_Scripts\Credentials-MDTJoinDomain.ps1" Write-Host "Domain joining to generic Computers OU" } } Function JoinDomainOU { # Perform domain join with filtered OU from above 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!" } } } if ((CheckDomainJoin) -eq $false) { FilterComputerOU JoinDomainOU } Stop-Transcript