win_join_ad.ps1
$HostName = Read-Host 'Please input HostName'
Write-Host 'HostName: ' $HostName
$Domain = 'Yudelei.com'
$TUser1 = 'Administrator'
Write-Host 'Local admin user number list: '
Write-Host ' 1: administrator'
Write-Host ' 2: other '
$TUserNumber = Read-Host 'Please input the local admin user number'
if ($TUserNumber -eq '1')
{ $TUser = $TUser1 }
else
{ $TUser = Read-Host 'Please input local admin user name'}
Write-Host 'Local admin: '$TUser
Write-Host 'Set local admin password.'
$Password = ConvertTo-SecureString -String "P@ssw0rd" -AsPlainText -Force
Set-LocalUser -Name $TUser -Password $Password
#Write-Host 'Set HostName.'
#Rename-Computer $HostName
$Credential = Get-Credential
if ($Credential){
try{Add-Computer -DomainName $Domain -NewName $HostName -Credential $Credential -Restart -Force}
catch{
Write-Host 'Remove PC in AD. '
Remove-Computer -UnjoinDomainCredential $Credential -PassThru -Verbose -Restart
}
}
else
{Write-Host 'No things to do. End.'}