none
How to catch the OpenError when removing and adding DHCP reservation RRS feed

  • Question

  • 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-DhcpServerv4Reservation and 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

    Monday, October 24, 2016 11:55 AM

Answers

  • Have you tried adding -ErrorAction Stop to the end of the Add-DhcpServerv4Reservation cmdlet in your try/catch block?

    -- Bill Stewart [Bill_Stewart]

    Monday, October 24, 2016 2:38 PM
    Moderator

All replies

  • Have you tried adding -ErrorAction Stop to the end of the Add-DhcpServerv4Reservation cmdlet in your try/catch block?

    -- Bill Stewart [Bill_Stewart]

    Monday, October 24, 2016 2:38 PM
    Moderator
  • Hi Bill,

    Thanks. It works.
    But for those having the same issue and wanted to repeat the Add-DhcpServerv4Reservation command until the DHCP database accepts the new entries, I have used the smart trick with $? which indicates an error when = $false. 

    The sample code looks like that:

    do {
      $IP |Add-DhcpServerv4Reservation -IPAddress $New_IP -ComputerName $dhcp_server
      if  (!($?)) { 
        Write-Host "The Add operation failed. Waiting 5s ...."
        Sleep 5
        Write-Host "Repeating ..."
        $error.clear()
      }
    } until ($?)

    I know that this doesn't handle all other issues and loop can work endless in other cases but it was quick and dirty fix to my issue. 

    Just from a curiosity can you do the same or better using Try/Catch?


    BR Mike

    Tuesday, October 25, 2016 11:26 AM