Quantcast
Channel: The Official Scripting Guys Forum! forum
Viewing all articles
Browse latest Browse all 15028

How to catch the OpenError when removing and adding DHCP reservation

$
0
0

Hi,

Got a script which "walks" through all IP reservations in a given Scope and rewrites them to another ones within the same Scope. I am using the Remove-DhcpServerv4Reservationand right after Add-DhcpServerv4Reservation command. It works OK for some IPs and suddenly it show an error for some IPs, and again it works for one or two and again shows the same error. 

Add-DhcpServerv4Reservation : Failed to reserve IP address 10.101.201.189 for ClientID/ClientDUID ec-b1-d7-95-2a-d0 in scope 10.101.0.0 on DHCP server dhcp01.local.
At C:\s.ps1:24 char:26+                 $lastIP |Add-DhcpServerv4Reservation -IPAddress $IP   -ComputerN ...+                          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+ CategoryInfo          : OpenError: (10.101.201.189:root/Microsoft/...erv4Reservation) [Add-DhcpServerv4Reservation], CimException+ FullyQualifiedErrorId : DHCP 20013,Add-DhcpServerv4Reservation

When looking at DHCP Server Management API Error Codes I can read that error 20013 is related with accessing the DHCP JET database. When triggered the action manually or put the Sleep 20 between Remove-... and Add-... command, more IPs were rewritten but still not all.

I wanted to catch this using try/catch in PowerShell but it did not catch this kind of error.

try {
 Remove-DhcpServerv4Reservation -IPAddress $IP.IPAddress.IPAddressToString -ComputerName $dhcp_server
 $IP |Add-DhcpServerv4Reservation -IPAddress $New_IP -ComputerName $dhcp_server
}
catch {
 Write-Host "Error occured"
 Break
}

Can you advise how the try / catch should look like to catch this kind of error or any error related with adding the IP reservation to the database?

Thanks in advance 

Mike


BR Michal Ziemba


Viewing all articles
Browse latest Browse all 15028

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>