Advertisement
Yevrag35

DHCP Scope/Option/Policy Migration

Jan 22nd, 2019
1,954
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #Requires -Version 3.0 -Modules DhcpServer -RunAsAdministrator
  2. [CmdletBinding(DefaultParameterSetName="CopyScopes")]
  3. param
  4. (
  5.     [parameter(Mandatory=$true, Position=0)]
  6.     [string] $NewDhcpServer,
  7.    
  8.     [parameter(Mandatory=$false)]
  9.     [switch] $CreateNewCimSession,
  10.    
  11.     [parameter(Mandatory=$false)]
  12.     [Microsoft.Management.Infrastructure.CimSession] $CimSession,
  13.  
  14.     [parameter(Mandatory=$false, Position=1)]
  15.     [pscredential] $Credential,
  16.  
  17.     [parameter(Mandatory=$false, Position=2)]
  18.     [string] $OldDhcpServer = $env:COMPUTERNAME,
  19.  
  20.     [parameter(Mandatory=$true, ParameterSetName="CopyScopes")]
  21.     [string] $NewPrefixName,
  22.  
  23.     [parameter(Mandatory=$false, ParameterSetName="CopyScopes")]
  24.     [switch] $IncludeInactiveScopes,
  25.  
  26.     [parameter(Mandatory=$true, ParameterSetName="CopyOptionsDefinitions")]
  27.     [switch] $PerformOptionsCopy,
  28.  
  29.     [parameter(Mandatory=$true, ParameterSetName="CopyServerPolicies")]
  30.     [switch] $PerformPoliciesCopy,
  31.  
  32.     [parameter(Mandatory=$false)]
  33.     [bool] $Confirm = $true,
  34.  
  35.     [parameter(Mandatory=$false)]
  36.     [switch] $Force
  37. )
  38.  
  39. $ra = @{ ErrorAction = "Stop" };
  40. if ($PSBoundParameters.ContainsKey("CreateNewCimSession"))
  41. {
  42.     $cimSes = New-CimSession -Authentication CredSsp -ComputerName $NewDhcpServer -Credential $Credential -ErrorAction Stop;
  43.     $ra.CimSession = $cimSes;
  44. }
  45. elseif ($PSBoundParameters.ContainsKey("CimSession"))
  46. {
  47.     $ra.CimSession = $CimSession;
  48. }
  49. else
  50. {
  51.     $ra.ComputerName = $NewDhcpServer;
  52. }
  53. $la = @{ ComputerName = $OldDhcpServer; ErrorAction = "Stop" };
  54.  
  55. #region Option Definitions Operation
  56. if ($PSBoundParameters.ContainsKey("PerformOptionsCopy"))
  57. {
  58.     # Recreate missing 'Option Definitions' on the new DHCP Server.
  59.     $optDefs = Get-DhcpServerv4OptionDefinition @la -All;
  60.     $newDefs = Get-DhcpServerv4OptionDefinition @ra -All;
  61.     $differences = Compare-Object -ReferenceObject $optDefs -DifferenceObject $newDefs -Property OptionId | `
  62.         Where-Object -FilterScript { $_.SideIndicator -eq '<=' } | `
  63.         Select-Object -ExpandProperty InputObject;
  64.     if (@($differences).Count -ne 0)
  65.     {
  66.         Write-Host "The following Options are " -f Yellow -NoNewline;
  67.         Write-Host "NOT" -f Red -NoNewline;
  68.         Write-Host " on the destination DHCP server:`n" -f Yellow
  69.         Write-Host "$([string]::Join(', ', [string[]]@($differences)))" -f Cyan;
  70.  
  71.         if ($PSBoundParameters.ContainsKey("Force"))
  72.         {
  73.             foreach ($def in $differences)
  74.             {
  75.                 $opt = Get-DhcpServerv4OptionDefinition @la -OptionId $def;
  76.                 Add-DhcpServerv4OptionDefinition @ra -Name $opt.Name -OptionId $opt.OptionId -Description $opt.Description `
  77.                     -Type $opt.Type -MultiValued:$opt.MultiValued -VendorClass $opt.VendorClass `
  78.                     -DefaultValue $opt.DefaultValue -Confirm:$Confirm;
  79.             }
  80.         }
  81.         else
  82.         {
  83.             Write-Host "`nTo add the missing DHCP server option definitions, re-run this script with the `"-Force`" switch." -f Yellow;
  84.         }
  85.     }
  86.     else
  87.     {
  88.         Write-Host "All Options are present on the destination DHCP server!" -f Green;
  89.     }
  90. }
  91. #endregion
  92.  
  93. #region Copy Scopes Operation
  94. if ($PSBoundParameters.ContainsKey("NewPrefixName"))
  95. {
  96.  
  97.     if (-not $PSBoundParameters.ContainsKey("IncludeNonActiveScopes"))
  98.     {
  99.         $scopes = Get-DhcpServerv4Scope @la | ? State -ne "Inactive"   # Get all scopes on old DHCP server.
  100.     }
  101.     else
  102.     {
  103.         $scopes = Get-DhcpServerv4Scope @la;    # Get all scopes on old DHCP server.
  104.     }
  105.  
  106.     $sCount = @($scopes).Count;
  107.     # Copy the scopes to the new server and leave in a 'Disabled' state.
  108.     for ($i = 1; $i -le $sCount; $i++)
  109.     {
  110.         $scope = $scopes[$i-1];
  111.         Write-Progress -Activity "Adding Scopes" -Status "Copying Scope $i/$sCount... $($scope.ScopeId)" `
  112.             -Id 1 -PercentComplete (($i/$sCount)*100);
  113.  
  114.         Add-DhcpServerv4Scope @ra -Name "$NewPrefixName - $($scope.Name)" -SubnetMask $scope.SubnetMask `
  115.             -StartRange $scope.StartRange -EndRange $scope.EndRange -State Inactive -Description $scope.Description `
  116.             -SuperscopeName $scope.SuperscopeName -MaxBootpClients $scope.MaxBootpClients -Type $scope.Type `
  117.             -ActivatePolicies $scope.ActivatePolicies -Delay $scope.Delay -LeaseDuration $scope.LeaseDuration `
  118.             -NapEnable:$scope.NapEnable -NapProfile $scope.NapProfile -Confirm:$Confirm;
  119.  
  120.         # Get all scope options and copy them to the new server's scope.
  121.         $sourceOpts = Get-DhcpServerv4OptionValue @la -ScopeId $scope.ScopeId -All;
  122.         $oCount = @($sourceOpts).Count;
  123.         for ($o = 1; $o -le $oCount; $o++)
  124.         {
  125.             $opt = $sourceOpts[$o-1];
  126.             Write-Progress -Activity "Adding Scope Options" -Status "Copying Option $o/$oCount..." `
  127.                 -Id 2 -ParentId 1 -PercentComplete (($o/$oCount)*100);
  128.  
  129.             Set-DhcpServerv4OptionValue @ra -ScopeId $scope.ScopeId -OptionId $opt.OptionId -Value $opt.Value -Confirm:$Confirm;
  130.         }
  131.         Write-Progress -Activity "Adding Scope Options" -Id 2 -Completed;
  132.  
  133.         # Get all the scope reservations and copy them to the new server's scope.
  134.         $sourceRes = Get-DhcpServerv4Reservation @la -ScopeId $scope.ScopeId;
  135.         $rCount = @($sourceRes).Count;
  136.         for ($r = 1; $r -le $rCount; $r++)
  137.         {
  138.             $resv = $sourceRes[$r-1];
  139.             Write-Progress -Activity "Adding Scope Reservations" -Status "Copying Reservation $r/$rCount... $($resv.IPAddress.ToString())" `
  140.                 -Id 3 -ParentId 1 -PercentComplete (($r/$rCount)*100);
  141.  
  142.             Add-DhcpServerv4Reservation @ra -ScopeId $scope.ScopeId -ClientId $resv.ClientId `
  143.                 -Name $resv.Name -Description $resv.Description -Type $resv.Type -IPAddress $resv.IPAddress -Confirm:$Confirm;
  144.         }
  145.         Write-Progress -Activity "Adding Scope Reservations" -Id 3 -Completed;
  146.  
  147.         # Get all the scope leases and copy them to the new server's scope.
  148.         $sourceLeases = Get-DhcpServerv4Lease @la -ScopeId $scope.ScopeId -AllLeases;
  149.         $lCount = @($sourceLeases).Count;
  150.         for ($l = 1; $l -le $lCount; $l++)
  151.         {
  152.             $lease = $sourceLeases[$l-1];
  153.             Write-Progress -Activity "Adding Scope Leases" -Status "Copying Lease $r/$rCount..." `
  154.                 -Id 4 -ParentId 1 -PercentComplete (($l/$lCount)*100);
  155.  
  156.             Add-DhcpServerv4Lease @ra -ScopeId $scope.ScopeId -IPAddress $lease.IPAddress -AddressState $lease.AddressState `
  157.                 -HostName $lease.HostName -Description $lease.Description -DnsRR $lease.DnsRR -DnsRegistration $lease.DnsRegistration `
  158.                 -ClientType $lease.ClientType -LeaseExpiryTime $lease.LeaseExpiryTime -NapCapable $lease.NapCapable -NapStatus $lease.NapStatus `
  159.                 -ProbationEnds $lease.ProbationEnds -ClientId $lease.ClientId -Confirm:$Confirm;
  160.         }
  161.         Write-Progress -Activity "Adding Scope Leases" -Id 4 -Completed;
  162.     }
  163.     Write-Progress -Activity "Adding Scopes" -Id 1 -Completed;
  164. }
  165. #endregion
  166.  
  167. #region Server Policies Operation
  168. if ($PSBoundParameters.ContainsKey("PerformPoliciesCopy"))
  169. {
  170.     Write-Host "Copying Server Policies... " -f Yellow -NoNewline;
  171.     try
  172.     {
  173.         $policies = Get-DhcpServerv4Policy @la;
  174.         $properties = @($policies)[0] | Get-Member -MemberType Property | Where-Object -FilterScript {
  175.             $_.Name -ne "PSComputerName"
  176.         };
  177.  
  178.         foreach ($p in $policies)
  179.         {
  180.             $hash = @{}
  181.             foreach ($prop in $properties)
  182.             {
  183.                 if (($p.$prop -is [string] -and [string]::IsNullOrEmpty($p.$prop)) -or ($p.$prop -isnot [string] -and $null -eq $p.$prop))
  184.                 {
  185.                     continue;
  186.                 }
  187.                 $hash.Add($prop, $p.$prop);
  188.             }
  189.             Add-DhcpServerv4Policy @ra @hash -Confirm:$Confirm;
  190.         }
  191.         Write-Host "Done!" -f Green;
  192.     }
  193.     catch
  194.     {
  195.         Write-Host "FAILED!`n" -f Red;
  196.         Write-Host $_.Exception.Message -f Red;
  197.     }
  198. }
  199. #endregion
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement