<commit_before>---
layout: post
title: PowerShell - Setup Dhcp Across Networks
description: How to use Windows Routing to enable a Dhcp server to work across different networks
---

<p>
  With all the benefits that DHCP has to offer, 
  it would be crazy if we needed to put a DHCP server in each 
  network we setup. 
  With this post, I will go through the steps of getting 
  your Windows DHCP server working across whatever networks you want.
</p>

### Prerequisites
----

To follow along with this guide you should have a setup with at least the following:
- Active Directory Forest
  - 1 Domain Controller
  - [Guide](https://codeandkeep.com/Dsc-Install-AD-Forest/)
- Dhcp server role on a server
  - Can be it's own server, or on a DC
  - [Guide](https://codeandkeep.com/PowerShell-Dhcp-Install/)
- RRAS role installed on a server
  - Should be on it's own server
  - [Guide](https://codeandkeep.com/PowerShell-Windows-Routing/)
- 1 test server on separate network

### My setup
----

2 networks:
  - 10.0.0.0
    - Has the Domain Controller (10.0.0.5)
    - Has the Dhcp server (10.0.0.3)
    - RRAS server: 
      - IP address: 10.0.0.1
      - NetAdapter Name: Ethernet0
  - 10.0.1.0
    - Has the test server
    - RRAS server:
      - IP address: 10.0.1.1
      - NetAdapter Name: Ethernet1


### The Journey
----

The configuration can be broken down into 3 steps:
  1. Get information about the Dhcp server (IP address, DNS host name)
  2. Create Dhcp scope for the new network
  3. Install and configure the Dhcp relay agent

#### 1. Get Dhcp server information
<p>
  We need to know the hostname or IP address of the Dhcp server. 
  We will also need to add a Dhcp scope for this new network 
  (10.0.1.1 in this example).
</p>

<p>
  You can run all the code directly from your RRAS server.
</p>

```powershell
# IP address or DnsHostName of your Dhcp Server
$dhcpServer = 'dhcp.codeAndKeep.com'

# Name of the network adapter to enable DHCP on
$routerNetAdapterName = 'Ethernet1'


$dhcpAddress = [Net.Dns]::GetHostEntry($dhcpServer)
if(!$dhcpAddress){
  Write-Warning "Unable to identify IP address of [$dhcpServer]"
  break
}else{
  $dhcpServerIP = $dhcpAddress.AddressList[0]
}
```

#### 2. Create new Dhcp scope
<commit_msg>Moved setences around for better readability<commit_after>---
layout: post
title: PowerShell - Setup Dhcp Across Networks
description: How to use Windows Routing to enable a Dhcp server to work across different networks
---

<p>
  With all the benefits that DHCP has to offer, 
  it would be crazy if we needed to put a DHCP server in each 
  network we setup. 
  With this post, I will go through the steps of getting 
  your Windows DHCP server working across whatever networks you want.
</p>

### Prerequisites
----

To follow along with this guide you should have a setup with at least the following:
- Active Directory Forest
  - 1 Domain Controller
  - [Guide](https://codeandkeep.com/Dsc-Install-AD-Forest/)
- Dhcp server role on a server
  - Can be it's own server, or on a DC
  - [Guide](https://codeandkeep.com/PowerShell-Dhcp-Install/)
- RRAS role installed on a server
  - Should be on it's own server
  - [Guide](https://codeandkeep.com/PowerShell-Windows-Routing/)
- 1 test server on new network

### My setup
----

2 networks:
  - 10.0.0.0
    - Has the Domain Controller (10.0.0.5)
    - Has the Dhcp server (10.0.0.3)
    - RRAS server: 
      - IP address: 10.0.0.1
      - NetAdapter Name: Ethernet0
  - 10.0.1.0
    - Has the test server
    - RRAS server:
      - IP address: 10.0.1.1
      - NetAdapter Name: Ethernet1


### The Journey
----

The configuration can be broken down into 3 steps:
  1. Get information about the Dhcp server (IP address, DNS host name)
  2. Create Dhcp scope for the new network
  3. Install and configure the Dhcp relay agent

<p>
  You can run all the code below directly from your RRAS server.
</p>

#### 1. Get Dhcp server information
<p>
  We need to know the hostname or IP address of the Dhcp server. 
  We will also need to add a Dhcp scope for this new network 
  (10.0.1.0 in this example).
</p>

```powershell
# IP address or DnsHostName of your Dhcp Server
$dhcpServer = 'dhcp.codeAndKeep.com'

# Name of the network adapter to enable DHCP on
$routerNetAdapterName = 'Ethernet1'


$dhcpAddress = [Net.Dns]::GetHostEntry($dhcpServer)
if(!$dhcpAddress){
  Write-Warning "Unable to identify IP address of [$dhcpServer]"
  break
}else{
  $dhcpServerIP = $dhcpAddress.AddressList[0]
}
```

#### 2. Create new Dhcp scope

{'fraction_non_alphanumeric': 0.09593280282935455, 'fraction_numerical': 0.021220159151193633, 'mean_word_length': 3.072907290729073, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 6, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 14, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4817866', 'n_tokens_mistral': 1554, 'n_tokens_neox': 1421, 'n_words': 606}