Windows Server 2019 Automation with PowerShell Cookbook
上QQ阅读APP看书,第一时间看更新

Installing and authorizing a DHCP server

In most organizations, your servers are configured with a static IP address configuration, but client computers can get IP addresses from a DHCP server. In Windows (and with most Linux, Macintosh, and mobile phones), the operating system contains a DHCP client that communicates with a DHCP server to obtain an IP address configuration (including the IP address, subnet mask, default gateway, and DNS server IP address).

Installing and authorizing a DHCP server is easy and straightforward. You can use Server Manager to achieve this. Server Manager, though, is a GUI that's layered on top of PowerShell. Alternatively, as you see in this recipe, you can use PowerShell to automate the installation and configuration of DHCP.

Getting ready

This recipe installs the DHCP service and the related management tools on the DC1.Reskit.Org computer. DC1 is a domain controller in the Reskit.Org domain and is also a DNS server.

How to do it...

  1. Install the DHCP server feature on DC1:
    Install-WindowsFeature -Name DHCP -IncludeManagementTools
  2. Add the DHCP server's security groups:
    Add-DHCPServerSecurityGroup -Verbose
  3. Let DHCP know that it's all configured:
    $RegHT = @{
      Path  = 'HKLM:\SOFTWARE\Microsoft\ServerManager\Roles\12'
      Name  = 'ConfigurationState'
      Value = 2
    }
    Set-ItemProperty @RegHT
  4. Authorize the DHCP server in AD:
    Add-DhcpServerInDC -DnsName DC1.Reskit.Org
  5. Restart DHCP:
    Restart-Service -Name DHCPServer –Force 

How it works...

In step 1, you used the Install-WindowsFeature cmdlet to add the DHCP server and the DHCP management tools, which looks like this:

In step 2, you added the necessary DHCP security groups. By default, this cmdlet does not produce any output. If you want to see some additional output, you can use the –Verbose switch. If you do, the cmdlet produces a bit of output, as follows:

In step 3, you told Windows that the configuration of DHCP is complete. This step produces no output, but is needed to let DHCP know that the necessary security groups are complete.

Before a DHCP server is able to provide IP address information to DHCP client hosts, you need to authorize it in AD. You performed this in step 4, which produces no output.

With the last step, step 5, you restarted the service. Since you authorized the DHCP server in the AD, the DHCP service can now start. After restarting, you can configure the DHCP server with address details and DHCP option values to distribute to DHCP clients.

There's more...

In step 1, you installed the DHCP server service on your system using the Install-WindowsFeature cmdlet. In earlier versions of the Server Manager PowerShell module, the cmdlet was named Add-WindowsFeature. In Windows Server 2019, Add-WindowsFeature is an alias for Install-WindowsFeature.

In step 2, you used the -Verbose switch. When you use the -Verbose switch with a cmdlet, you can get some additional output that shows you what the cmdlet (or function) is doing. The amount of extra information returned when using the -Verbose switch depends on the cmdlet. Some cmdlets are remarkably terse and provide little or no extra verbose output. Other cmdlets provide more detailed verbose output.

In step 4, you authorized the DHCP server explicitly in the Active Directory. Authorization helps your organization avoid the potential for a rogue user setting up a DHCP server and handing out inappropriate IP address details. If you have multiple domain controllers, you may wish to force AD replication so that all DCs show this server as authorized. While the replication should occur pretty quickly, it never hurts to check the replication status before enabling the DHCP service (as you do in step 5).