Use PowerShell to modify the DNS and add domain

Operating environment: Windows Server 2012 R2

 

This powershell script to automatically modify the machine and join the domain DNS

But sometimes you will be prompted local computer is not currently a part of [the domain. Please re-run the script! ] Error, in case the error may follow the prompts to re-execute the script

If after repeatedly executed or given the wrong script because the machine join the domain, but the domain controller in the machine is not due to appear

 

Ultimate solution: Quit manually add the domain after domain, in this way a high probability to find a domain controller in the machine

 

# Automatically modify the local DNS and added to the domain 
# author: lttr <www.cnblogs.com/GoCircle>
# DATE: 2019-08-09
# . EG # C: \ domain.ps1-DNS "1.1.1.1", "2.2.2.2" -Domain "test.com" -UserName "ADMIN" -Password "123" [the CmdletBinding ()] param ( # the DNS list, may be provided two [the Parameter (Mandatory = $ to true , the Position = 0)] [String []] $ the DNS , # domain [the Parameter (Mandatory = $ to true , the Position =. 1)] [String] $ the domain , # domain accounts [the Parameter (Mandatory = $ to true , the Position = 2)] [String] $ UserName , #Domain account password [the Parameter (Mandatory = $ to true , the Position =. 3)] [the SecureString] $ Password ) $ WMI = WmiObject to the Get-Win32_NetworkAdapterConfiguration - filter " IPEnabled = 'to true' " # Set the DNS   $ WMI .SetDNSServerSearchOrder ( $ the DNS )> null $ # verify that the DNS setting iF (( $ WMI .DNSServerSearchOrder | the ForEach -object { $ DNS -Match $ _ .}) the Count -eq $ DNS .Count) { the Write -host " DNS success " #加入域 $pwd = $Password | ConvertTo-SecureString -asPlainText -Force $credential = New-Object System.Management.Automation.PSCredential("$Domain\$UserName",$pwd) $server = (Get-WmiObject -Class Win32_ComputerSystem | Select-Object Name).Name Add-Computer -ComputerName $server -DomainName $Domain -Credential $credential -PassThru -Verbose -ErrorAction SilentlyContinue -WarningAction silentlyContinue Start-Sleep -Seconds 3 # 测试是否加入到域中 {the try IF (the Test-ComputerSecureChannel -Server " $ $ the Domain Server. " ) { the Reset -ComputerMachinePassword -Server $ the Domain -Credential $ Credential Write -host Success! PAUSE the Restart -Computer - Force } the else { the Write -warning " Local Computer Current not part of the domain. Please re-execute the script! " } } {the catch the Write -warning " part of the local computer is not currently a domain. Please re-execute the script! " } } the else{ The Write -warning " the DNS settings fail, please re-run the script! " }

 

 

Guess you like

Origin www.cnblogs.com/GoCircle/p/11328320.html