Configuring Windows Server 2016 core as a DHCP Server with PowerShell
February 15th, 2017 by Charlie Russel and tagged Active Directory, AD DS, DHCP, Networking, PowerShell, Server core
As I mentioned last time, I’m setting up a new domain controller and DHCP server for my internal domain on Windows Server 2016 Core, and I’m exclusively using PowerShell to do it. For both the DHCP Server and AD DS roles, we need to configure a fixed IP address on the server, so let’s do that first. From my Deploying and Managing Active Directory with Windows PowerShell book from Microsoft Press, here’s my little very quick and dirty script to set a fixed IP address:
# Quick and dirty IP address setter [CmdletBinding()] Param ([Parameter(Mandatory=$True)][string]$IP4, [Parameter(Mandatory=$True)][string]$IP6 ) $Network = "192.168.10." $Network6 = "2001:db8:0:10::" $IPv4 = $Network + "$IP4" $IPv6 = $Network6 + "$IP6" $Gateway4 = $Network + "1" $Gateway6 = $Network6 + "1" Write-Verbose "$network,$network6,$IP4,$IP6,$IPv4,$IPv6,$gateway4, $gateway6" $Nic = Get-NetAdapter -name Ethernet $Nic | Set-NetIPInterface -DHCP Disabled $Nic | New-NetIPAddress -AddressFamily IPv4 ` -IPAddress $IPv4 ` -PrefixLength 24 ` -type Unicast ` -DefaultGateway $Gateway4 Set-DnsClientServerAddress -InterfaceAlias $Nic.Name ` -ServerAddresses 192.168.10.2,2001:db8:0:10::2 $Nic | New-NetIPAddress -AddressFamily IPv6 ` -IPAddress $IPv6 ` -PrefixLength 64 ` -type Unicast ` -DefaultGateway $Gateway6 ipconfig /all
I warned you it was a quick and dirty script. But let’s quickly look at what it does. First, we get the network adapter into a variable, $Nic. Then we turn off DHCP with Set-NetIPInterface, and configure the IPv4 and IPv6 addresses with New-NetIPAddress. Finally, we use Set-DnsClientServerAddress to configure the DNS Servers for this server.
Next, let’s join the server to the TreyResearch.net domain with another little script. OK, I admit, you could do this all as a simple one-liner, but I do it so often that I scripted it.
<# .Synopsis Joins a computer to the domain .Description Joins a new computer to the domain. If the computer hasn't been renamed yet, it renames it as well. .Parameter NewName The new name of the computer .Parameter Domain The domain to join the computer to. Default value is TreyResearch.net .Example Join-myDomain -NewName trey-wds-11 .Example Join-myDomain dc-contoso-04 -Domain Contoso.com .Notes Name: Join-myDomain Author: Charlie Russel Copyright: 2017 by Charlie Russel : Permission to use is granted but attribution is appreciated ModHist: 9 Apr, 2014 -- Initial : 25 Feb, 2015 -- Updated to allow name already matches : #> [CmdletBinding()] Param ( [Parameter(Mandatory=$true,Position=0)] [String]$NewName, [Parameter(Mandatory=$false,Position=1)] [String]$Domain = "TreyResearch.net" ) $myCred = Get-Credential -UserName "$Domain\Charlie" ` -Message "Enter the Domain password for Charlie." if ($ENV:COMPUTERNAME -ne $NewName ) { Add-Computer -DomainName $Domain -Credential $myCred -NewName $NewName -restart } else { Add-Computer -DomainName $Domain -Credential $myCred -Restart }
After the server restarts, log in with your domain credentials, not as “Administrator”. The account you logon with should be at least Domain Admin or equivalent, since you’re going to be adding DHCP to the server and promoting it to be a domain controller.
To add the necessary roles to the server, use:
Install-WindowsFeature -Name DHCP,AD-Domain-Services ` -IncludeAllSubFeature ` -IncludeManagementTools
Next, download updated Get-Help files with Update-Help. Once you’ve got those, go ahead and restart the server, and when it comes back up, we’ll do the base configuration for DHCP to enable it in the domain, and create the necessary accounts. Creating scopes, etc., is the topic of another day. Probably as part of my Lab series.
First, enable the DHCP server in AD (this assumes the $NewName from earlier was ‘trey-core-03’. )
Add-DhcpServerInDC -DnsName 'trey-core-03' -PassThru
And, finally, create the necessary local groups:
# Create local groups for DHCP # The WinNT in the following IS CASE SENSITIVE $connection = [ADSI]"WinNT://trey-core-03" $lGroup = $connection.Create("Group","DHCP Administrators") $lGroup.SetInfo() $lGroup = $connection.Create("Group","DHCP Users") $lGroup.SetInfo()
This uses ADSI to create a local group, since there’s no good way built into base PowerShell to do it except through ADSI.
Finally, we’ll use my Promote-myDC.ps1 script to promote the server to domain controller. Again, I could easily do this by hand, but I’m building and rebuilding labs often enough that I scripted it. I’m lazy! Do it once, use the PowerShell interactive command line. Do it twice? Write a script!
<# .Synopsis Tests a candidate domain controller, and then promotes it to DC. .Description Promote-myDC first tests if a domain controller can be successfully promoted, and, if the user confirms that the test was successful, completes the promotion and restarts the new domain controller. .Example Promote-myDC -Domain TreyResearch.net Tests if the local server can be promoted to domain controller for the domain TreyResearch.net. The user is prompted after the test completes and must press the Y key to continue the promotion. .Parameter Domain The domain to which the server will be promoted to domain controller. .Inputs [string] .Notes Author: Charlie Russel Copyright: 2017 by Charlie Russel : Permission to use is granted but attribution is appreciated Initial: 05/14/2016 (cpr) ModHist: 02/14/2017 (cpr) Default the domain name for standard lab builds : #> [CmdletBinding()] Param( [Parameter(Mandatory=$False,Position=0)] [string]$Domain = 'TreyResearch.net' ) Write-Verbose "Testing if ADDSDeployment module is available" If ( (Get-WindowsFeature -Name AD-Domain-Services).InstallState -ne "Installed" ) { Write-Verbose "Installing the ActiveDirectory Windows Feature, since you seem to have forgotten that." Install-WindowsFeature -Name AD-Domain-Services -IncludeManagementTools Write-Host "" } If ( (Get-WindowsFeature -Name AD-Domain-Services).InstallState -ne "Installed" ) { throw "Failed to install the ActiveDirectory Windows Feature." } Write-Verbose "Testing if server $env:computername can be promoted to DC in the $Domain domain" Write-Host "" Test-ADDSDomainControllerInstallation ` -NoGlobalCatalog:$false ` -CreateDnsDelegation:$false ` -CriticalReplicationOnly:$false ` -DatabasePath "C:\Windows\NTDS" ` -DomainName $Domain ` -LogPath "C:\Windows\NTDS" ` -NoRebootOnCompletion:$false ` -SiteName "Default-First-Site-Name" ` -SysvolPath "C:\Windows\SYSVOL" ` -InstallDns:$true ` -Force Write-Host "" Write-Host "" Write-Host "" Write-Host -NoNewLine "If the above looks correct, press Y to continue... " $Key = [console]::ReadKey($true) $sKey = $key.key Write-Verbose "The $sKey key was pressed." Write-Host "" Write-Host "" If ( $sKey -eq "Y" ) { Write-Host "The $sKey key was pressed, so proceeding with promotion of $env:computername to domain controller." Write-Host "" sleep 5 Install-ADDSDomainController ` -SkipPreChecks ` -NoGlobalCatalog:$false ` -CreateDnsDelegation:$false ` -CriticalReplicationOnly:$false ` -DatabasePath "C:\Windows\NTDS" ` -DomainName $Domain ` -InstallDns:$true ` -LogPath "C:\Windows\NTDS" ` -NoRebootOnCompletion:$false ` -SiteName "Default-First-Site-Name" ` -SysvolPath "C:\Windows\SYSVOL" ` -Force:$true } else { Write-Host "The $sKey key was pressed, exiting to allow you to fix the problem." Write-Host "" Write-Host "" }
This uses a little trick I haven’t talked about before –
$Key = [console]::ReadKey($true) $sKey = $key.key
This reads in a single keystroke and gets the value of the key. Because of the way this works, “Y” and “y” are equivalent. Useful to give yourself a last chance out if something doesn’t look right, though obviously you’ll want to remove those bits if you’re creating a script that needs to run without interactive input.
Posted in Active Directory, DHCP, Hyper-V, Networking, PowerShell, Windows Server 2016, Windows Server Core | Comments Off on Configuring Windows Server 2016 core as a DHCP Server with PowerShell