• Add DHCP Reservations with a Script

    by  • 2011/01/26 • Tech • 6 Comments

    It was time to upgrade DHCP at one of my clients.   They had a lot of address reservations and we were setting up split-zone DHCP so I didn’t want to have to enter all the reservations by hand, twice…

    There had to be a way to script this…
    PowerShell (before version 3) didn’t manage DHCP, but it can be used to create a batch file with the NETSH commands. 🙂

    ##==============================================================================<br />
    ##==============================================================================<br />
    ## SCRIPT.........: Create-Reservation.ps1<br />
    ## AUTHOR.........: Clint McGuire<br />
    ## EMAIL..........:<br />
    ## VERSION........: 1<br />
    ## DATE...........: 2011-1-23<br />
    ## COPYRIGHT......: 2011, Clint McGuire<br />
    ## LICENSE........:<br />
    ## REQUIREMENTS...: Powershell v2.0<br />
    ##<br />
    ## DESCRIPTION....: Creates an CMD file to add reservations to DHCP.<br />
    ##<br />
    ## NOTES..........: Requires CSV file with 3 fields, IP, MAC and NAME<br />
    ##<br />
    ## CUSTOMIZE......:<br />
    ##==============================================================================<br />
    ## START<br />
    ##==============================================================================<br />
    $DHCPServer = "10.0.0.1"<br />
    $DHCPscope = "10.0.0.0"<br />
    $outputfile = "C:\netsh-output.cmd"


    # Assumes a CSV with three columns, MAC, IP and NAME.
    $IPs = import-csv “C:\PSlistofips.csv”


    $IPs | foreach 
    {
    add-content -Encoding ASCII -Path $outputfile -Value “netsh Dhcp Server $DHCPServer Scope $DHCPScope Add reservedip $($_.IP) $($_.MAC) `”$($_.NAME)`” `”$($_.NAME)`” `”BOTH`””
    }


    ##==============================================================================
    ## END
    ##==============================================================================

    The output in netsh-output.cmd looks like this:
    netsh Dhcp Server 10.0.0.1 Scope 10.0.0.0 Add reservedip 10.0.0.11 00AA11BB22CC "compname.domain.local" "compname.domain.local" "BOTH"


    Executing the CMD file add the reservations to DHCP matching the IP address and MAC to the computer name. The computer name is listed twice so that it is the Reservation name and the description.

    Notes:
    -The add-content cmdlet appends data to the CMD file, if you need to run the PS1 multiple times you will have to delete or rename your CMD file between runs.
    -The MAC address needs to be specified without dashes or colons between pairs.

    About

    Clint McGuire is a Computer Consultant based out of Vancouver Canada. He specializes in VMware and Storage.

    6 Responses to Add DHCP Reservations with a Script

    1. 2011/07/26 at 5:34 AM

      Hi, Thanks for this. Have the same scenario. Two servers sharing dhcp but have to manually add reservations all the time.
      Cheers

    2. 2012/03/30 at 1:05 PM

      Thanks for this. I was pulling my hair out knowing there had to be a way to use the csv file I had to make these reservations!

    3. Roberto Dimas
      2012/04/09 at 1:13 PM

      It didn’t work in powershell for me and I was in a hurry, so I had to do it with this little perl script, following the same logic:

      #filename: dodhcp.pl
      while(){
      chomp;
      @values=split (“,”,$_);
      print “netsh Dhcp Server 11.0.0.1 Scope 11.0.0.0 Add Reservedip $values[5] $values[6] \”$values[1]\” \”$values[1]\” \”BOTH\”\n”;
      }
      You have to run it like this

      perl dodhcp.pl dhcp.cmd

      and then run dhcp.cmd to make the actual reservations.

    4. Pingback: Powershell script for creating DHCP reservation batch file | SethBlog

    5. Gotcha
      2013/11/28 at 8:46 PM

      Thanks man..it’s work flawlessly, safe my time a lot…good job..

      • Clint
        2013/11/28 at 10:11 PM

        You are welcome. Glad it was helpful.

    Leave a Reply

    Your email address will not be published. Required fields are marked *