Skip to content

Commit 027626c

Browse files
Update Transfer-DHCPScopes.ps1
Signed-off-by: LUIZ HAMILTON ROBERTO DA SILVA <[email protected]>
1 parent 36ad1be commit 027626c

1 file changed

Lines changed: 39 additions & 3 deletions

File tree

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

Lines changed: 39 additions & 3 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+
v3.5 – July 24, 2025
1414
#>
1515

1616
#region --- Hide Console ---
@@ -132,6 +132,32 @@ function Export-DhcpScope {
132132
Write-Log "Removed scope $ScopeId from $Server"
133133
}
134134

135+
# Wait for the file to be fully released
136+
$maxWait = 5
137+
$waited = 0
138+
while ($waited -lt $maxWait -and -not (Test-Path $exportFile)) {
139+
Start-Sleep -Seconds 1
140+
$waited++
141+
}
142+
143+
# Optional: confirm file is no longer locked
144+
$unlocked = $false
145+
for ($i = 0; $i -lt 5; $i++) {
146+
try {
147+
$stream = [System.IO.File]::Open($exportFile, 'Open', 'Read', 'None')
148+
$stream.Close()
149+
$unlocked = $true
150+
break
151+
} catch {
152+
Start-Sleep -Milliseconds 500
153+
}
154+
}
155+
156+
if (-not $unlocked) {
157+
throw "Export file is still locked after multiple attempts: $exportFile"
158+
}
159+
160+
Write-Log "Export file is ready and unlocked: $exportFile"
135161
$ProgressBar.Value = 100
136162
return $true
137163
} catch {
@@ -155,8 +181,10 @@ function Import-DhcpScope {
155181
throw "Import file not found: $ImportFilePath"
156182
}
157183

158-
[xml]$xmlContent = Get-Content $ImportFilePath
159-
$existingScopes = Get-DhcpServerv4Scope -ComputerName $Server -ErrorAction Stop | Select-Object -ExpandProperty ScopeId
184+
# Load and parse XML
185+
[xml]$xmlContent = [xml](Get-Content -Path $ImportFilePath -Raw -ErrorAction Stop)
186+
$existingScopes = Get-DhcpServerv4Scope -ComputerName $Server -ErrorAction Stop |
187+
Select-Object -ExpandProperty ScopeId
160188

161189
$xmlScopeNodes = $xmlContent.DHCPServer.IPv4.Scopes.Scope
162190
$scopesToImport = @()
@@ -174,9 +202,13 @@ function Import-DhcpScope {
174202
Write-Log "All scopes in $ImportFilePath already exist on $Server. No import performed." -Level "INFO"
175203
$ProgressBar.Value = 100
176204
$null = [Windows.Forms.MessageBox]::Show("All scopes already exist on $Server.`nNothing was imported.", "Info", "OK", "Information")
205+
# Cleanup object before exit
206+
Remove-Variable -Name xmlContent -Force -ErrorAction SilentlyContinue
207+
[System.GC]::Collect()
177208
return "Skipped"
178209
}
179210

211+
# Proceed with import
180212
Import-DhcpServer -ComputerName $Server -File $ImportFilePath -Leases -BackupPath $global:ExportDir -ErrorAction Stop
181213
Write-Log "Imported scopes from $ImportFilePath to $Server"
182214

@@ -187,6 +219,10 @@ function Import-DhcpScope {
187219
}
188220
}
189221

222+
# Cleanup and return
223+
Remove-Variable -Name xmlContent -Force -ErrorAction SilentlyContinue
224+
[System.GC]::Collect()
225+
190226
$ProgressBar.Value = 100
191227
return "Imported"
192228
} catch {

0 commit comments

Comments
 (0)