97 lines
3.6 KiB
PowerShell
97 lines
3.6 KiB
PowerShell
<#
|
|
.Synopsis
|
|
Script to Automated DHCP Migration
|
|
.DESCRIPTION
|
|
Takes DHCP Configuration from Source server and converts to Destination (local server) configuration.
|
|
.EXAMPLE
|
|
.\migrate-dhcp.ps1 -source sbs2011server
|
|
.EXAMPLE
|
|
.\migrate-dhcp.ps1 -source sbs2011server -detailed
|
|
#>
|
|
param(
|
|
[Parameter(Mandatory=$True,Position=0)]
|
|
[string]$source,
|
|
[switch]$detailed
|
|
)
|
|
$destination = $env:Computername
|
|
$sourceIp = [System.Net.Dns]::GetHostAddresses($source)
|
|
$sourceIP = $sourceIP.IPAddressToString
|
|
$currentIP = get-netIPConfiguration
|
|
$destIntAlias = $currentIP.InterfaceAlias
|
|
$destIP = $currentIP.IPv4Address.IPAddress.ToString()
|
|
$destIPv6 = $currentIP.ipv6linklocaladdress.ipAddress
|
|
Write-Output "DHCP Migration "
|
|
Write-Output "Source : $source"
|
|
Write-Output "Source IP : $sourceIP"
|
|
Write-Output "Destination : $destination"
|
|
Write-Output "Destination IP : $destIP"
|
|
Write-Output "Destination IPv6 : $destIPv6"
|
|
Write-Output "Reading DHCP Configuration.. Please Wait"
|
|
$dhcpConfig = netsh dhcp server "\\$source" dump
|
|
if(($dhcpConfig) -like "Unable*")
|
|
{
|
|
Write-Output "Error Collecting DHCP Configuration : $source"
|
|
Write-Output "Check DHCP Service is Running on Source Server!"
|
|
}
|
|
else
|
|
{
|
|
Write-Output "Processing DHCP Configuration.. Please Wait"
|
|
foreach ($line in $dhcpConfig)
|
|
{
|
|
|
|
if(($line) -like "dhcp*")
|
|
{
|
|
try
|
|
{
|
|
if($line.Contains($source))
|
|
{
|
|
$line = $line.Replace("$source","$destination")
|
|
}
|
|
if($line.Contains($sourceIp))
|
|
{
|
|
if($line.Contains("Add iprange") -or ($line.Contains("optionvalue 3") -or ($line.Contains("add excluderange"))))
|
|
{
|
|
# Skip Line Changes
|
|
}
|
|
else
|
|
{
|
|
$line = $line.Replace("$sourceIP","$destIP")
|
|
}
|
|
}
|
|
if($line.Contains("23 IPV6ADDRESS"))
|
|
{
|
|
$line = $line.Split("""")
|
|
$sourcev6 = $line[1]
|
|
$line = $line.Replace("$sourcev6","$destIPv6")
|
|
$line = $line.Replace("%3","")
|
|
}
|
|
if($line.Contains("Local Area Connection"))
|
|
{
|
|
$line = $line.Replace("Local Area Connection","$destIntAlias")
|
|
}
|
|
$command = "netsh $line"
|
|
if($detailed)
|
|
{
|
|
Write-Output $command
|
|
}
|
|
cmd /c $command | Out-Null
|
|
}
|
|
catch
|
|
{
|
|
$errorMessage = $_.exception.message
|
|
}
|
|
}
|
|
}
|
|
Add-DhcpServerInDC -DnsName $destination -IPAddress $destIP
|
|
Set-DhcpServerv4DnsSetting -ComputerName $destination -DynamicUpdates Always -DeleteDnsRROnLeaseExpiry $true
|
|
$username = "$env:UserDomain\$env:userName"
|
|
$pwd = Read-Host -AsSecureString -Prompt "Enter Password for $username for DHCP/DNS Update"
|
|
$cred = New-Object System.Management.Automation.PSCredential($username,$pwd)
|
|
Set-DHCPServerDNSCredential $cred
|
|
Set-DHCPServerSetting -ConflictDetectionAttempts 2
|
|
Write-Output "Disable DHCP Service on : $source"
|
|
Write-Output "Stop DHCP Service on : $source"
|
|
Get-Service -ComputerName $source -ServiceName dhcpserver | Set-Service -StartupType Disabled
|
|
Get-Service -ComputerName $source -ServiceName dhcpserver | Stop-Service -Force
|
|
Write-Output "DHCP Configuration Complete"
|
|
} |