Skip to content

Instantly share code, notes, and snippets.

@andrewbbrown
Last active June 21, 2018 04:17
Show Gist options
  • Save andrewbbrown/376a5901dfb40c8888dfee1db182a1b7 to your computer and use it in GitHub Desktop.
Save andrewbbrown/376a5901dfb40c8888dfee1db182a1b7 to your computer and use it in GitHub Desktop.
This creates AD forest...
try {
# Boxstarter options
$Boxstarter.RebootOk=$true
$Boxstarter.NoPassword=$false # Is this a machine with no logon password?
$Boxstarter.AutoLogin=$true
Write-BoxstarterMessage "Setting Windows Explorer Settings!!"
Set-WindowsExplorerOptions -EnableShowHiddenFilesFoldersDrives -EnableShowProtectedOSFiles -EnableShowFileExtensions -EnableShowFullPathInTitleBar
Enable-RemoteDesktop
# Install AD
Write-BoxstarterMessage "Installing AD Server Windows Feature!!"
##Install-WindowsFeature 'AD-Domain-Services' -IncludeAllSubFeature -IncludeManagementTools
Get-WindowsFeature AD-Domain-Services | Install-WindowsFeature -IncludeAllSubFeature -IncludeManagementTools
Function Set-IPAddress {
param (
[string]$Name = "Ethernet0",
[IPAddress]$IP = "192.168.99.11",
[string] $CIDR = 24, # This means subnet mask = 255.255.255.0,
[string]$Gateway = "192.168.99.10",
[string]$Dns = "192.168.99.10,8.8.8.8",
[string]$IPType = "IPv4",
[string]$Type = "Static",
[string]$NewName = "Management"
)
# Retrieve the network adapter that you want to configure
$adapter = Get-NetAdapter | ? {$_.Name -eq $Name}
if ($Type -eq "Static") {
# Remove any existing IP, gateway from our ipv4 adapter
If (($adapter | Get-NetIPConfiguration).IPv4Address.IPAddress) {
Write-Host "Removing existing IP"
$adapter | Remove-NetIPAddress -AddressFamily $IPType -Confirm:$false
}
If (($adapter | Get-NetIPConfiguration).Ipv4DefaultGateway) {
Write-Host "Removing existing gateway"
$adapter | Remove-NetRoute -AddressFamily $IPType -Confirm:$false
}
# Configure the IP address and default gateway
Write-Host "Configuring new IP"
$adapter | New-NetIPAddress `
-AddressFamily $IPType `
-IPAddress $IP `
-PrefixLength $CIDR `
-DefaultGateway $Gateway
# Configure the DNS client server IP addresses
Write-Host "Configuring new gateway"
$adapter | Set-DnsClientServerAddress -ServerAddresses $DNS
}
else {
$interface = $adapter | Get-NetIPInterface -AddressFamily $IPType
If ($interface.Dhcp -eq "Disabled") {
# Remove existing gateway
Write-Host "Removing existing gateway"
If (($interface | Get-NetIPConfiguration).Ipv4DefaultGateway) {
$interface | Remove-NetRoute -Confirm:$false
}
# Enable DHCP
Write-Host "Enabling DHCP on interface"
$interface | Set-NetIPInterface -DHCP Enabled
# Configure the DNS Servers automatically
Write-Host "Enabling automatic DNS"
$interface | Set-DnsClientServerAddress -ResetServerAddresses
}
}
Write-Host "Restarting adapter"
$adapter | Restart-NetAdapter
$adapter | Rename-NetAdapter -NewName $NewName
$adapter = Get-NetAdapter | ? {$_.Name -eq $NewName}
$adapter | Restart-NetAdapter
}
Set-IPAddress -Name "Ethernet0" -NewName "Management" -IP "192.168.99.11" -Gateway "192.168.99.10" -Dns "192.168.99.10" -IPType "IPv4" -Type "Static"
# Rename Computer
$ServerName = "DC1"
$HostName = $($env:computername).ToUpper()
if ($HostName -ne $ServerName) { Rename-Computer -NewName $ServerName -restart }
# Install DNS
Write-BoxstarterMessage "Installing DNS Server Windows Feature!!"
#Install-WindowsFeature 'DNS' -IncludeManagementTools
Get-WindowsFeature DNS | Install-WindowsFeature -IncludeAllSubFeature -IncludeManagementTools
#Enable AD
# Installing ADDS Forest that is DNS connected
Write-BoxstarterMessage "Creating Active Directory Forest!!"
$newDomainName="foobar99.local"
$newDomainNetBios="foobar99"
try{ $IsAdForestOnline = Get-ADForest -Server $newDomainName | Select -ExpandProperty "Name" }catch{ $IsAdForestOnline = $null }
if($IsAdForestOnline -ne "foobar99.local")
{
Install-ADDSForest -DomainName $newDomainName -InstallDns -DomainNetbiosName $newDomainNetBios -SafeModeAdministratorPassword (ConvertTo-SecureString "VMware1!" -AsPlainText -Force) -Force
}
##savingjustincase##Install-ADDSForest -DomainName $newDomainName -InstallDns -DomainNetbiosName $newDomainNetBios -SafeModeAdministratorPassword (ConvertTo-SecureString "VMware1!" -AsPlainText -Force) -Force
## did not work becasue DNS delegation does not exist...
## Install-ADDSForest -DomainName $newDomainName -CreateDNSDelegation -DomainNetbiosName $newDomainNetBios -SafeModeAdministratorPassword (ConvertTo-SecureString "VMware1!" -AsPlainText -Force) -Force
Write-BoxstarterMessage "Waiting for DNS Server to come back online! Sleeping 60 seconds"
Start-Sleep -s 60
#for($i=1; $i -le 10; $i++)
#{
# $GetDnsServerTestResult = Test-DnsServer -ZoneName foobar99.local -IPAddress 192.168.99.11 | Select -ExpandProperty "Result"
# if($GetDnsServerTestResult -eq "Success"){$i = 10; Write-BoxstarterMessage "DNS Server Detected!!"}
# else { Start-Sleep -s 30 }
# if($i -eq 9)
# {shutdown -r; Start-Sleep -s 60;}
#}
Write-BoxstarterMessage "Creating DNS Zone and A Records!!"
Add-DnsServerPrimaryZone -NetworkId "192.168.99.0/24" -ReplicationScope "Forest"
Add-DnsServerResourceRecordA -Name "HL2-VYOS-01" -IPv4Address "192.168.99.2" -ZoneName "foobar99.local" -AllowUpdateAny -CreatePtr -TimeToLive 00:30:00
Add-DnsServerResourceRecordA -Name "HL1-ESXI-01" -IPv4Address "192.168.99.20" -ZoneName "foobar99.local" -AllowUpdateAny -CreatePtr -TimeToLive 00:30:00
# Adding DNS records for HL2 Windows Servers
Add-DnsServerResourceRecordA -Name "HL1-ISCSI-01" -IPv4Address "192.168.99.30" -ZoneName "foobar99.local" -AllowUpdateAny -CreatePtr -TimeToLive 00:30:00
Add-DnsServerResourceRecordA -Name "HL2-SQL-01" -IPv4Address "192.168.99.31" -ZoneName "foobar99.local" -AllowUpdateAny -CreatePtr -TimeToLive 00:30:00
Add-DnsServerResourceRecordA -Name "HL2-IAAS-01" -IPv4Address "192.168.99.32" -ZoneName "foobar99.local" -AllowUpdateAny -CreatePtr -TimeToLive 00:30:00
Add-DnsServerResourceRecordA -Name "HL2-SMB-01" -IPv4Address "192.168.99.33" -ZoneName "foobar99.local" -AllowUpdateAny -CreatePtr -TimeToLive 00:30:00
# Adding DNS records for HL2 vMware Server Appliances
Add-DnsServerResourceRecordA -Name "HL2-VCSA-01" -IPv4Address "192.168.99.40" -ZoneName "foobar99.local" -AllowUpdateAny -CreatePtr -TimeToLive 00:30:00
#Install DHCP Server
Write-BoxstarterMessage "Installing DHCP Server Windows Feature!!"
Get-WindowsFeature DHCP | Install-WindowsFeature -IncludeAllSubFeature -IncludeManagementTools
# Create an IPv4 DHCP Server Scope
#Add-DhcpServerv4Scope -Name "ManagementNetwork" -StartRange 192.168.10.67.36.2 -EndRange 10.67.36.254 -SubnetMask 255.255.255.0 -#LeaseDuration 00.00:30:30 -State Active
# Set the Router Option value
#Set-DhcpServerv4OptionValue -ScopeId 10.67.36.0 -DnsServer 10.67.36.4 -DnsDomain foobar.local -Router 10.67.36.2
## This didn't work at all and can be skipped...
### Create DHCP Option definition 150 for TFTP
##Add-DhcpServerv4OptionDefinition -OptionId 150 -Type IPv4Address -Name "TFTP-Server"
#Set the DHCP Option Value for Option 150
#Set-DhcpServerv4OptionValue -ScopeId "10.67.36.0" -OptionId 150 -Value 10.67.36.33
#Adding a DHCP Server Reservation via MAC ID address (ClientId)
# HL1-ESXI-01 (AKA THE XEON SERVER with MAC 001E677AC303)
#Add-DhcpServerv4Reservation -Name HL1-ESXI-01 -IPAddress 10.67.36.20 -ClientId 001E677AC303 -ScopeId 10.67.36.0
Write-BoxstarterMessage "Machine is complete!"
} catch {
Write-ChocolateyFailure 'Boxstarter Error: ' $($_.Exception.Message)
throw
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment