Powershell

Table of Contents

2.4 DHCP

DHCP (Dynamic Host Configuration Protocol) is an protocol used for IP address distribution in a network. DHCP configures the default gateway, subnet mask, DNS & IP Address of a networked device.

DHCP is administered as a service role in Windows Server Operating System. Once the DHCP role is deployed, whether through the easy wizard GUI setup, or through PowerShell, an administrator can use PowerShell or GUI to administer IP Addressing on the network to interconnected devices as well as run reporting on statistics of IP Addressed devices.

A DHCP Server is commonly installed as a service as part of Windows Server Operating System, but DHCP is also found in routers and other operating systems.

How it works

When a client with internet connection capabilities connects to a unknown network with a DHCP server, it sends a signal 'DHCPDISCOVER' to the DHCP server. The DHCP server then responds with the policies and settings for the network and assigns a leased IP address for use 'DHCPOFFER'. The client acknowledges the information then asks for permission to use the assigned IP address 'DHCPREQUEST '.The DHCP server acknowledges the request then the client is able to connect to the network.

An DHCP leased IP Address may also be statically assigned against an MAC (Media Access Control) Address of a client. The MAC address is a 48-bit or 64-bit address associated with a network adapter assigned by a governing Internet body to all network capable devices. Static address will not expire thereby allowing a client to keep its IP Address for an indefinite period of time allowing a control for access, e.g. a website hosted on a particular computer.

DHCP can also be used to segregate and exclude certain MAC devices from connecting to the network as well as create various organisational units to segregate clients by subnets allowing easier management of IP Addressing. All this can be done within PowerShell. Knowing how DHCP works, one can use PowerShell to deploy/assign/report/administer IP Addressing on a network.

DHCP Module In Windows Server 2008 and newer Operating Systems you will first have to import the PowerShell module.
Import-module dhcpserver


For the full list of DHCP Module commands see Microsoft's Knowledge Base repository here

Setup DHCP

In the steps below we are running commands to install DHCP on a Windows Server Instance while on the device itself. Your able to run remote commands to also perform the below. For network 192.168.1.0 a standard Class C network with 255 IP Address to use, we will use the following IP Addressing to setup.

Network: 192.168.1.0
Subnet Mask: 255.255.255.0/24
Default Gateway: 192.168.1.1
First IP Address: 192.168.1.2
Last IP Address: 192.168.1.254
Broadcast address: 129.168.1.255

using the above details we will setup DHCP configured to the above Network range. Then you will be able to assign out IP Address using PowerShell or the GUI from Windows Server Operating System, Server tools application to start the DHCP Snappin.

If you don’t know how to setup a basic network IP Addressing scheme, please see reference here

1) Install DHCP Server Role

Install-WindowsFeature -Name 'DHCP' –IncludeManagementTools


2) Add DHCP Scope

Add-DhcpServerV4Scope -Name "DHCP Scope" -StartRange 192.168.1.2 -EndRange 192.168.1.254 -SubnetMask 255.255.255.0


3) Add DNS Server & Router Gateway Options within DHCP

Set-DhcpServerV4OptionValue -DnsServer 192.168.1.10 -Router 192.168.1.1


4) Set Up Lease Duration

Set-DhcpServerv4Scope -ScopeId 192.168.1.10 -LeaseDuration 1.00:00:00


5) Restart DHCP Service

Restart-service dhcpserver



Get All DHCP Subnets (Requires PowerShell 4)

Get-DHCPServerv4scope



Get all DHCP Subnet Leases

Get-DHCPServerv4scope | Get-DHCPServerv4Lease



Visit next page to learn about - 2.5 DNS