Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Requires -Version 3.0 -Modules DhcpServer -RunAsAdministrator
- [CmdletBinding(DefaultParameterSetName="CopyScopes")]
- param
- (
- [parameter(Mandatory=$true, Position=0)]
- [string] $NewDhcpServer,
- [parameter(Mandatory=$false)]
- [switch] $CreateNewCimSession,
- [parameter(Mandatory=$false)]
- [Microsoft.Management.Infrastructure.CimSession] $CimSession,
- [parameter(Mandatory=$false, Position=1)]
- [pscredential] $Credential,
- [parameter(Mandatory=$false, Position=2)]
- [string] $OldDhcpServer = $env:COMPUTERNAME,
- [parameter(Mandatory=$true, ParameterSetName="CopyScopes")]
- [string] $NewPrefixName,
- [parameter(Mandatory=$false, ParameterSetName="CopyScopes")]
- [switch] $IncludeInactiveScopes,
- [parameter(Mandatory=$true, ParameterSetName="CopyOptionsDefinitions")]
- [switch] $PerformOptionsCopy,
- [parameter(Mandatory=$true, ParameterSetName="CopyServerPolicies")]
- [switch] $PerformPoliciesCopy,
- [parameter(Mandatory=$false)]
- [bool] $Confirm = $true,
- [parameter(Mandatory=$false)]
- [switch] $Force
- )
- $ra = @{ ErrorAction = "Stop" };
- if ($PSBoundParameters.ContainsKey("CreateNewCimSession"))
- {
- $cimSes = New-CimSession -Authentication CredSsp -ComputerName $NewDhcpServer -Credential $Credential -ErrorAction Stop;
- $ra.CimSession = $cimSes;
- }
- elseif ($PSBoundParameters.ContainsKey("CimSession"))
- {
- $ra.CimSession = $CimSession;
- }
- else
- {
- $ra.ComputerName = $NewDhcpServer;
- }
- $la = @{ ComputerName = $OldDhcpServer; ErrorAction = "Stop" };
- #region Option Definitions Operation
- if ($PSBoundParameters.ContainsKey("PerformOptionsCopy"))
- {
- # Recreate missing 'Option Definitions' on the new DHCP Server.
- $optDefs = Get-DhcpServerv4OptionDefinition @la -All;
- $newDefs = Get-DhcpServerv4OptionDefinition @ra -All;
- $differences = Compare-Object -ReferenceObject $optDefs -DifferenceObject $newDefs -Property OptionId | `
- Where-Object -FilterScript { $_.SideIndicator -eq '<=' } | `
- Select-Object -ExpandProperty InputObject;
- if (@($differences).Count -ne 0)
- {
- Write-Host "The following Options are " -f Yellow -NoNewline;
- Write-Host "NOT" -f Red -NoNewline;
- Write-Host " on the destination DHCP server:`n" -f Yellow
- Write-Host "$([string]::Join(', ', [string[]]@($differences)))" -f Cyan;
- if ($PSBoundParameters.ContainsKey("Force"))
- {
- foreach ($def in $differences)
- {
- $opt = Get-DhcpServerv4OptionDefinition @la -OptionId $def;
- Add-DhcpServerv4OptionDefinition @ra -Name $opt.Name -OptionId $opt.OptionId -Description $opt.Description `
- -Type $opt.Type -MultiValued:$opt.MultiValued -VendorClass $opt.VendorClass `
- -DefaultValue $opt.DefaultValue -Confirm:$Confirm;
- }
- }
- else
- {
- Write-Host "`nTo add the missing DHCP server option definitions, re-run this script with the `"-Force`" switch." -f Yellow;
- }
- }
- else
- {
- Write-Host "All Options are present on the destination DHCP server!" -f Green;
- }
- }
- #endregion
- #region Copy Scopes Operation
- if ($PSBoundParameters.ContainsKey("NewPrefixName"))
- {
- if (-not $PSBoundParameters.ContainsKey("IncludeNonActiveScopes"))
- {
- $scopes = Get-DhcpServerv4Scope @la | ? State -ne "Inactive" # Get all scopes on old DHCP server.
- }
- else
- {
- $scopes = Get-DhcpServerv4Scope @la; # Get all scopes on old DHCP server.
- }
- $sCount = @($scopes).Count;
- # Copy the scopes to the new server and leave in a 'Disabled' state.
- for ($i = 1; $i -le $sCount; $i++)
- {
- $scope = $scopes[$i-1];
- Write-Progress -Activity "Adding Scopes" -Status "Copying Scope $i/$sCount... $($scope.ScopeId)" `
- -Id 1 -PercentComplete (($i/$sCount)*100);
- Add-DhcpServerv4Scope @ra -Name "$NewPrefixName - $($scope.Name)" -SubnetMask $scope.SubnetMask `
- -StartRange $scope.StartRange -EndRange $scope.EndRange -State Inactive -Description $scope.Description `
- -SuperscopeName $scope.SuperscopeName -MaxBootpClients $scope.MaxBootpClients -Type $scope.Type `
- -ActivatePolicies $scope.ActivatePolicies -Delay $scope.Delay -LeaseDuration $scope.LeaseDuration `
- -NapEnable:$scope.NapEnable -NapProfile $scope.NapProfile -Confirm:$Confirm;
- # Get all scope options and copy them to the new server's scope.
- $sourceOpts = Get-DhcpServerv4OptionValue @la -ScopeId $scope.ScopeId -All;
- $oCount = @($sourceOpts).Count;
- for ($o = 1; $o -le $oCount; $o++)
- {
- $opt = $sourceOpts[$o-1];
- Write-Progress -Activity "Adding Scope Options" -Status "Copying Option $o/$oCount..." `
- -Id 2 -ParentId 1 -PercentComplete (($o/$oCount)*100);
- Set-DhcpServerv4OptionValue @ra -ScopeId $scope.ScopeId -OptionId $opt.OptionId -Value $opt.Value -Confirm:$Confirm;
- }
- Write-Progress -Activity "Adding Scope Options" -Id 2 -Completed;
- # Get all the scope reservations and copy them to the new server's scope.
- $sourceRes = Get-DhcpServerv4Reservation @la -ScopeId $scope.ScopeId;
- $rCount = @($sourceRes).Count;
- for ($r = 1; $r -le $rCount; $r++)
- {
- $resv = $sourceRes[$r-1];
- Write-Progress -Activity "Adding Scope Reservations" -Status "Copying Reservation $r/$rCount... $($resv.IPAddress.ToString())" `
- -Id 3 -ParentId 1 -PercentComplete (($r/$rCount)*100);
- Add-DhcpServerv4Reservation @ra -ScopeId $scope.ScopeId -ClientId $resv.ClientId `
- -Name $resv.Name -Description $resv.Description -Type $resv.Type -IPAddress $resv.IPAddress -Confirm:$Confirm;
- }
- Write-Progress -Activity "Adding Scope Reservations" -Id 3 -Completed;
- # Get all the scope leases and copy them to the new server's scope.
- $sourceLeases = Get-DhcpServerv4Lease @la -ScopeId $scope.ScopeId -AllLeases;
- $lCount = @($sourceLeases).Count;
- for ($l = 1; $l -le $lCount; $l++)
- {
- $lease = $sourceLeases[$l-1];
- Write-Progress -Activity "Adding Scope Leases" -Status "Copying Lease $r/$rCount..." `
- -Id 4 -ParentId 1 -PercentComplete (($l/$lCount)*100);
- Add-DhcpServerv4Lease @ra -ScopeId $scope.ScopeId -IPAddress $lease.IPAddress -AddressState $lease.AddressState `
- -HostName $lease.HostName -Description $lease.Description -DnsRR $lease.DnsRR -DnsRegistration $lease.DnsRegistration `
- -ClientType $lease.ClientType -LeaseExpiryTime $lease.LeaseExpiryTime -NapCapable $lease.NapCapable -NapStatus $lease.NapStatus `
- -ProbationEnds $lease.ProbationEnds -ClientId $lease.ClientId -Confirm:$Confirm;
- }
- Write-Progress -Activity "Adding Scope Leases" -Id 4 -Completed;
- }
- Write-Progress -Activity "Adding Scopes" -Id 1 -Completed;
- }
- #endregion
- #region Server Policies Operation
- if ($PSBoundParameters.ContainsKey("PerformPoliciesCopy"))
- {
- Write-Host "Copying Server Policies... " -f Yellow -NoNewline;
- try
- {
- $policies = Get-DhcpServerv4Policy @la;
- $properties = @($policies)[0] | Get-Member -MemberType Property | Where-Object -FilterScript {
- $_.Name -ne "PSComputerName"
- };
- foreach ($p in $policies)
- {
- $hash = @{}
- foreach ($prop in $properties)
- {
- if (($p.$prop -is [string] -and [string]::IsNullOrEmpty($p.$prop)) -or ($p.$prop -isnot [string] -and $null -eq $p.$prop))
- {
- continue;
- }
- $hash.Add($prop, $p.$prop);
- }
- Add-DhcpServerv4Policy @ra @hash -Confirm:$Confirm;
- }
- Write-Host "Done!" -f Green;
- }
- catch
- {
- Write-Host "FAILED!`n" -f Red;
- Write-Host $_.Exception.Message -f Red;
- }
- }
- #endregion
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement