Skip to content

Commit ec714e1

Browse files
Update Transfer-DHCPScopes.ps1
Signed-off-by: LUIZ HAMILTON ROBERTO DA SILVA <[email protected]>
1 parent 8bc1f2c commit ec714e1

1 file changed

Lines changed: 51 additions & 23 deletions

File tree

SysAdmin-Tools/Network-and-Infrastructure-Management/Transfer-DHCPScopes.ps1

Lines changed: 51 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
Luiz Hamilton Silva - @brazilianscriptguy
1111
1212
.VERSION
13-
v3.5 – July 23, 2025
13+
v4.5 – July 23, 2025
1414
#>
1515

1616
#region --- Hide Console ---
@@ -150,17 +150,41 @@ function Import-DhcpScope {
150150
)
151151
try {
152152
$ProgressBar.Value = 10
153+
153154
if (-not (Test-Path $ImportFilePath)) {
154155
throw "Import file not found: $ImportFilePath"
155156
}
156157

158+
[xml]$xmlContent = Get-Content $ImportFilePath
159+
$existingScopes = Get-DhcpServerv4Scope -ComputerName $Server -ErrorAction Stop | Select-Object -ExpandProperty ScopeId
160+
161+
$xmlScopeNodes = $xmlContent.DHCPServer.IPv4.Scopes.Scope
162+
$scopesToImport = @()
163+
164+
foreach ($scopeNode in $xmlScopeNodes) {
165+
$scopeId = $scopeNode.ScopeId
166+
if ($existingScopes -contains $scopeId) {
167+
Write-Log "Skipping import of scope $scopeId – already exists on $Server" -Level "WARNING"
168+
} else {
169+
$scopesToImport += $scopeId
170+
}
171+
}
172+
173+
if ($scopesToImport.Count -eq 0) {
174+
Write-Log "All scopes in $ImportFilePath already exist on $Server. No import performed." -Level "INFO"
175+
[Windows.Forms.MessageBox]::Show("All scopes already exist on $Server.`nNothing was imported.", "Info", "OK", "Information")
176+
$ProgressBar.Value = 100
177+
return $true
178+
}
179+
157180
Import-DhcpServer -ComputerName $Server -File $ImportFilePath -Leases -BackupPath $global:ExportDir -ErrorAction Stop
158181
Write-Log "Imported scopes from $ImportFilePath to $Server"
159182

160-
if ($InactivateAfter -and ($ImportFilePath -match "Export-Scope_([\d\.]+)_")) {
161-
$scopeId = $matches[1]
162-
Set-DhcpServerv4Scope -ComputerName $Server -ScopeId $scopeId -State Inactive
163-
Write-Log "Inactivated scope $scopeId after import"
183+
if ($InactivateAfter) {
184+
foreach ($newScopeId in $scopesToImport) {
185+
Set-DhcpServerv4Scope -ComputerName $Server -ScopeId $newScopeId -State Inactive -ErrorAction Stop
186+
Write-Log "Inactivated scope $newScopeId after import"
187+
}
164188
}
165189

166190
$ProgressBar.Value = 100
@@ -330,26 +354,30 @@ function Show-GUI {
330354
})
331355

332356
$btnImport.Add_Click({
333-
$barImport.Value = 0
334-
$lblStatusImport.Text = "Validating inputs..."
335-
if (-not $txtImpServer.Text -or -not $txtImpFile.Text) {
336-
[Windows.Forms.MessageBox]::Show("Specify server and import file", "Error", "OK", "Error")
337-
$lblStatusImport.Text = "Import failed: Missing inputs"
338-
return
339-
}
357+
$barImport.Value = 0
358+
$lblStatusImport.Text = "Validating inputs..."
340359

341-
$lblStatusImport.Text = "Importing scope..."
342-
$success = Import-DhcpScope -Server $txtImpServer.Text -ImportFilePath $txtImpFile.Text `
343-
-InactivateAfter:$chkInactivateImp.Checked -ProgressBar $barImport
360+
$serverName = $txtImpServer.Text.Trim()
361+
$importFile = $txtImpFile.Text.Trim()
344362

345-
if ($success) {
346-
[Windows.Forms.MessageBox]::Show("Scope imported to $txtImpServer.Text", "Success", "OK", "Information")
347-
$lblStatusImport.Text = "Import completed"
348-
} else {
349-
[Windows.Forms.MessageBox]::Show("Import failed. Check log.", "Error", "OK", "Error")
350-
$lblStatusImport.Text = "Import failed"
351-
}
352-
})
363+
if (-not $serverName -or -not $importFile) {
364+
[Windows.Forms.MessageBox]::Show("Specify both server and import file.", "Error", "OK", "Error")
365+
$lblStatusImport.Text = "Import failed: Missing inputs"
366+
return
367+
}
368+
369+
$lblStatusImport.Text = "Importing scope..."
370+
$success = Import-DhcpScope -Server $serverName -ImportFilePath $importFile `
371+
-InactivateAfter:$chkInactivateImp.Checked -ProgressBar $barImport
372+
373+
if ($success) {
374+
[Windows.Forms.MessageBox]::Show("Scope import process completed on server:`n$serverName", "Success", "OK", "Information")
375+
$lblStatusImport.Text = "Import completed"
376+
} else {
377+
[Windows.Forms.MessageBox]::Show("Import failed. Check log for details.", "Error", "OK", "Error")
378+
$lblStatusImport.Text = "Import failed"
379+
}
380+
})
353381

354382
# Initial button state
355383
$btnExport.Enabled = $false

0 commit comments

Comments
 (0)