|
10 | 10 | Luiz Hamilton Silva - @brazilianscriptguy |
11 | 11 |
|
12 | 12 | .VERSION |
13 | | - v3.5 – July 23, 2025 |
| 13 | + v4.5 – July 23, 2025 |
14 | 14 | #> |
15 | 15 |
|
16 | 16 | #region --- Hide Console --- |
@@ -150,17 +150,41 @@ function Import-DhcpScope { |
150 | 150 | ) |
151 | 151 | try { |
152 | 152 | $ProgressBar.Value = 10 |
| 153 | + |
153 | 154 | if (-not (Test-Path $ImportFilePath)) { |
154 | 155 | throw "Import file not found: $ImportFilePath" |
155 | 156 | } |
156 | 157 |
|
| 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 | + |
157 | 180 | Import-DhcpServer -ComputerName $Server -File $ImportFilePath -Leases -BackupPath $global:ExportDir -ErrorAction Stop |
158 | 181 | Write-Log "Imported scopes from $ImportFilePath to $Server" |
159 | 182 |
|
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 | + } |
164 | 188 | } |
165 | 189 |
|
166 | 190 | $ProgressBar.Value = 100 |
@@ -330,26 +354,30 @@ function Show-GUI { |
330 | 354 | }) |
331 | 355 |
|
332 | 356 | $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..." |
340 | 359 |
|
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() |
344 | 362 |
|
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 | +}) |
353 | 381 |
|
354 | 382 | # Initial button state |
355 | 383 | $btnExport.Enabled = $false |
|
0 commit comments