Setup-ADForest.ps1

<#PSScriptInfo
 
.VERSION 1.0.0
 
.GUID b4e4a291-d183-48b7-8110-f0cf3ecbce21
 
.AUTHOR MosaicMK Software LLC
 
.COMPANYNAME MosaicMK Software LLC
 
.COPYRIGHT (c) 2018 MosaicMK Software LLC. All rights reserved
 
.TAGS Windows, Server, AD, Active Directory
 
.LICENSEURI https://opensource.org/licenses/MS-PL
 
.PROJECTURI https://www.mosaicmk.com
 
.ICONURI https://3.bp.blogspot.com/-5AH8bMtdvcU/XBpsEqKMoFI/AAAAAAAABIw/cRbUnQwTwdIpZapoCD4ifYatBmy717zSgCLcBGAs/s1600/logo-transparent_NoWords.png.ico
 
.EXTERNALMODULEDEPENDENCIES
 
.REQUIREDSCRIPTS
 
.EXTERNALSCRIPTDEPENDENCIES
 
.RELEASENOTES
 
.PRIVATEDATA
 
#>


<#
.SYNOPSIS
Setup and configure a new AD Forest
.DESCRIPTION
Setup and configure a new AD Forest
.PARAMETER DomainName
Name of the new domain
.PARAMETER NetBiosName
NetBIOS name of the domain
.PARAMETER NTDSPath
Path to where NTDS database is to be stored (Defaults to %SystemDrive%\Windows\NTDS)
.PARAMETER LogPath
Path to log file for the setup proccess (Defaults to %SystemDrive%\ForestSetup)
.PARAMETER SYSVolPath
Path to where to keep SYSVol (Defaults to %SystemDrive%\Windows\SYSVol)
.PARAMETER DisableFirewall
Disables the firewall for Domain, Private, public or all profiles
.PARAMETER SetStaticIP
Set a staic IP Address on the selected network card
.PARAMETER NetAdapter
Network Adatper to configure
.PARAMETER IPAddress
IP Address to set on the network adpter
.PARAMETER Subnetmask
Subnet mask on the network adpter
.PARAMETER DefaultGateway
Default gateway on the network adapter
.PARAMETER InstallDHCP
Install DHCP on the server (Be sure to configure a static IP address)
.PARAMETER Restart
Restarts the server after install has completed
.EXAMPLE
.\Setup-ADForest.ps1 -DomainName MosaicMK.local -NetBisoName MOSAICMK -NTDSPath C:\NTDS -SYSVolPath C:\SYSVol
Installs the Forest Mosacimk.local placing the NTDS database in C:\NTDS and SYSVol in C:\SYSVol
.EXAMPLE
.\Setup-ADForest.ps1 -DomainName MosaicMK.local -NetBisoName MOSAICMK -NTDSPath C:\NTDS -SYSVolPath C:\SYSVol -SetStaticIP -NetAdapter "Local Network" -IPAddress 192.168.1.25 -Subnetmask 24 -DefaultGateway 192.168.1.1
Installs the Forest Mosacimk.local placing the NTDS database in C:\NTDS and SYSVol in C:\SYSVol and
configures the Local Network adapter with a static ip address
.EXAMPLE
.\Setup-ADForest.ps1 -DomainName MosaicMK.local -NetBisoName MOSAICMK -NTDSPath C:\NTDS -SYSVolPath C:\SYSVol -InstallDHCP
.NOTES
Contact: Contact@mosaicmk.com
Facebook: MosaicMK Software LLC
Version 1.0.0
.LINK
http://www.mosaicmk.com
#>

PARAM(
    [Parameter(Mandatory=$true)]
    [string]$DomainName,
    [Parameter(Mandatory=$true)]
    [string]$NetBiosName,
    [string]$NTDSPath = "$ENV:SystemDrive\Windows\NTDS",
    [string]$SYSVolPath = "$ENV:SystemDrive\Windows\NTDS",
    [string]$LogPath = "$ENV:SystemDrive\ForestSetup",
    [ValidateSet('Domain','Private','Public','All')]
    [string]$DisableFirewall,
    [switch]$SetStaticIP,
    [string]$NetAdapter,
    [string]$IPAddress,
    [String]$Subnetmask,
    [String]$DefaultGateway,
    [switch]$InstallDHCP,
    [switch]$Restart
)

IF ($DisableFirewall){IF ($DisableFirewall -eq "All"){Set-NetFirewallProfile -Enabled:false} else {Set-NetFirewallProfile -Enabled:false -Name $DisableFirewall}}
IF ($SetStaticIP){
    IF (!($IPAddress)){$IPAddress = Read-Host "IPAddress"}
    IF (!($Subnetmask)){$Subnetmask = Read-Host "Subnetmask (ex: 24)"}
    IF (!($DefaultGateway)){$DefaultGateway = Read-Host "Default Gateway"}
    IF (!($NetAdapter)){Get-NetAdapter;$NetAdapter = Read-Host "Name of netowrk adapter"
    }
    New-NetIPAddress -IPAddress $IPAddress -DefaultGateway $DefaultGateway -PrefixLength $Subnetmask -InterfaceIndex (Get-NetAdapter -Name $NetAdapter).InterfaceIndex
    Set-DnsClientServerAddress -InterfaceIndex (Get-NetAdapter -Name $NetAdapter).InterfaceIndex -ServerAddresses 127.0.0.1
}

try {
    Install-WindowsFeature AD-Domain-Services -IncludeManagementTools -ErrorAction Stop
    Import-Module ADDSDeployment -ErrorAction Stop
    Install-ADDSForest -CreateDnsDelegation:$false -DatabasePath "$NTDSPath" -DomainMode "Default" -DomainName "$DomainName" -DomainNetbiosName "$NetBiosName" -ForestMode "Default" -InstallDns -LogPath "$LogPath" -NoRebootOnCompletion -SysvolPath "$SYSVolPath" -Force -ErrorAction Stop
} Catch {Write-Error "$_";Exit 1}

IF ($InstallDHCP){
    try {
        IF (!($IPAddress)){$IPAddress = Read-Host "IPAddress"}
        If (!($DomainName)){$DomainName = Read-Host "Domain Name"}
        $Type = (Get-NetIPAddress -IPAddress $IPAddress).PrefixOrigin
        IF ($Type -ne "Manual"){Write-Error "The selected network adapter is not using a static IP Address";exit 1}
        $DHCPName = $ENV:COMPUTERNAME + "." + $DomainName
        Install-WindowsFeature DHCP -IncludeManagementTools -ErrorAction Stop
        netsh dhcp add securitygroups
        Restart-service dhcpserver
        Add-DHCPServerInDC -DnsName $DHCPName -IPAddress $IPAddress -ErrorAction Stop
        Set-DhcpServerv4DnsSetting -ComputerName "$DHCPName" -DynamicUpdates "Always" -DeleteDnsRRonLeaseExpiry $True
    }catch {Write-Error "$_"}
}

if ($Restart){Restart-Computer -Force}