DHCP – Powershell – Migrate DHCP Scope Leases

This simple script shows you how to migrate a scopes leases from  one DHCP server to another,disable the old scope and enable the new one

Assumptions:

Windows Server 2012 R2 or higher

Destination DHCP server is already setup with Scopes scope options and reservations, but scopes disabled

Destination has no existing leases and is inactive

Destination

IP Helpers are already setup and proven working

 

###############
#change these
$SourceDHCPServer = "Server1"

$DestinationDHCPServer = "Server2"

$ScopeToMigrate  = "10.134.102.0"
###############

#Get the Source  Scope
$ScopeA = Get-DhcpServerv4Scope -ComputerName $SourceDHCPServer -scopeID $ScopeToMigrate

#Make sure the destination is InActive
Set-DhcpServerv4Scope -ComputerName $DestinationDHCPServer -ScopeId $ScopeToMigrate  -State InActive

#Get the Leases the source Servers, but exclude reservations
$LeasesSourceServerA = Get-DhcpServerv4Lease -ComputerName $SourceDHCPServer -ScopeId $ScopeToMigrate -AllLeases |Where-Object {($_.AddressState -notlike "*Reservation*")}


#Now lets do the leases slowly as they seem to fail randomly, 1 sec sleep to see if doing it slowly and individually makes it any better

Write-Host "Leases"
foreach ($item in $LeasesSourceServerA)
{
   $item | Add-DhcpServerv4Lease -ComputerName $DestinationDHCPServer -ScopeId $ScopeToMigrate 
   Start-sleep 1
   Write-Host $item.IPAddress $item.clientID 
}

#Set Source Scope InActive
Set-DhcpServerv4Scope -ComputerName $SourceDHCPServer -ScopeId $ScopeToMigrate  -State InActive
#Set Destination Scope Active
Set-DhcpServerv4Scope -ComputerName $DestinationDHCPServer -ScopeId $ScopeToMigrate  -State Active

#

 

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.