How to Set Up Active Directory and DHCP with PowerShell

Adam Bertram

Adam Bertram

Read more posts by this author.

I’m currently building a test lab for an upcoming Powershell course. I’m trying to do EVERYTHING for this test lab in PowerShell. Normally, I’ll just get lazy and go back to my GUI ways sometimes when I don’t know the cmdlet off hand. Anyway, I’ll try to share some of the scripts I’m using to get everything up and going. Here’s my first contribution.

To preface this set of scripts, I’ve got a blank Windows Server 2012 R2 server using DHCP for its IP. I want to make this server an Active Directory domain controller and a DHCP server to get my test lab started.

Alot of this was inspired by this Hey Scripting Guy blog post.

PrepOS.ps1

## Set a static IP
$ipaddress = '192.168.10.20'
$ipprefix = '24'
$ipgw = '192.168.10.1'
$ipdns = '192.168.10.20'
$ipif = (Get-NetAdapter).ifIndex
New-NetIPAddress -IPAddress $ipaddress -PrefixLength $ipprefix� -InterfaceIndex $ipif

## Set the NIC to use itself as the DNS server since this is going to be a domain controller
Set-DnsClientServerAddress -InterfaceIndex $ipif -ServerAddresses $ipdns

## Rename the server and restart it to commit the change
$newname = 'TEST-DC'
Rename-Computer -NewName $newname -force
Restart-Computer -Force

SetupDomainController.ps1

## Install the bits for Active Directory Domain Services
Install-WindowsFeature 'AD-Domain-Services'� -IncludeAllSubFeature -IncludeManagementTools

# Create New Forest, add Domain Controller
$domainname = 'test.local'
$netbiosName = 'TEST'
Import-Module ADDSDeployment

$ad_params = @{
    CreateDnsDelegation=$false;
    DatabasePath='C:NTDS';
    DomainMode='Win2012';
    DomainName=$domainname;
    DomainNetbiosName=$netbiosName;
    ForestMode='Win2012';
    InstallDns=$true;
    NoRebootOnCompletion=$false;
    Force=$true;
    SafeModeAdministratorPassword=(ConvertTo-SecureString '[email protected]$$w0rd12' -AsPlainText -Force);
}

Install-ADDSForest @ad_params

InstallDHCPServer.ps1

## Install the bits on the server for DHCP
Install-WindowsFeature 'DHCP' -IncludeAllSubFeature -IncludeManagementTools

## Authorize the DHCP server in Active Directory
Add-DhcpServerInDC -DnsName test-dc.test.local -IPAddress 192.168.10.20

## Create an IPv4 DHCP scope
Add-DhcpServerv4Scope -Name 'Test Scope' -StartRange '192.168.10.100' -EndRange '192.168.10.250' -SubnetMask '255.255.255.0'

## Set the DNS server to use for all clients to use
on the DHCP server
Set-DhcpServerv4OptionValue -DnsDomain test.local -DnsServer 192.168.10.20

Subscribe to Adam the Automator

Get the latest posts delivered right to your inbox

Looks like you're offline!