Home > Computers > Backup DHCP with PowerShell Script

Backup DHCP with PowerShell Script

I found this script a while back on Jason Carter’s blog but it’s no longer active so I figured I’d post it here for reference. Be sure to change any paths to fit your environment.

#*****************************************************************
#
#   Script Name:  dhcpBackup.ps1
#   Version:  1.0
#   Author:  Jason Carter
#
#   Description:  Used to backup DHCP logs from the DHCP server
#   to another location for archiving purposes.
#
#***************************************************************** 

#Get Yestedays Date In Month, Day, Year format
$yesterday=(get-date (get-date).AddDays(-1) -uformat %Y%m%d)

#Get the first 3 letters of the day name from yesterday
$logdate=([string]((get-date).AddDays(-1).DayofWeek)).substring(0,3)

#Change path to DHCP log folder, copy yesterdays log file to backup location
cd C:\Windows\System32\dhcp
copy "DhcpSrvLog-$logdate.log" \\SERVER\SHARE\DHCParchive

#Rename log file with yesterdays date
cd \\SERVER\SHARE\DHCParchive
rename-item "DhcpSrvLog-$logdate.log" "$yesterday.log"

#Dump DHCP database
$today=(get-date -uformat %Y%m%d)
$dumpfile="DHCP_DUMP-$today.txt"
netsh dhcp server \\DC dump > \\SERVER\SHARE\DHCParchive\$dumpfile
Categories: Computers Tags: ,
  1. max
    March 2, 2011 at 2:58 am

    Yes it’s good but my server is installed in french language, when you gate-date it’s displayed Mon Sun for exemple do I have an error message to copy this log, because I have french OS day of week diplayed like this Lun Mar Mer Ven Sam
    do you have any solution
    thanks

    • patrickhoban
      March 2, 2011 at 3:03 pm

      I’m not familiar with how a couple of things appear in non-English versions of Windows so a few questions.

      1. If you run the following PowerShell command what is the output?
      ([string]((get-date).AddDays(-1).DayofWeek)).substring(0,3)

      2. In C:\Windows\System32\dhcp does your log for Monday show up as DhcpSrvLog-Mon.log or DhcpSrvLog-Lun.log?

  2. Eduardo
    March 15, 2013 at 3:39 am

    $logdate=(get-Date (get-date).AddDays(-1) -format dddd).Substring(0,3)

  3. Pad
    February 26, 2014 at 11:38 am

    Hi Patrick,
    what about running it on multiple servers (servers.txt) and apend the hostname+date to filename and copy each log to \\server\sharename\DHCParchive\hostname folder?
    Thanks in advance.

  4. Pad
    February 26, 2014 at 2:21 pm

    I have tried by adding the below to the script, but only the date apears, hostname is ignored in the filename:

    #Get hostname
    $computer = gc env:computername
     
    #Change path to DHCP log folder, copy yesterdays log file to backup location and rename
    cd C:\Windows\System32\dhcp
    copy “DhcpSrvLog-$logdate.log” \\SERVER\e$\LogRepos\DHCP\$computer\”$computer_$yesterday.log”

  5. Pad
    February 27, 2014 at 3:09 am

    I solved it:

    copy “DhcpSrvLog-$logdate.log” \\SERVER\e$\LogRepos\DHCP\$computer\””$computer”_”$yesterday”.log”

    • patrickhoban
      February 27, 2014 at 11:24 am

      Nice. Thanks for the follow-up.

  6. June 2, 2016 at 6:29 am

    the last command netsh dhcp server \\DC dump > produces this result
    The following command was not found: dhcp server \\DC dump.
    Help required

    Regards,
    Animalvideos.pk

    • patrickhoban
      June 8, 2016 at 2:21 pm

      Did you change \\DC to the name of your DHCP server?

  7. Josh
    June 22, 2016 at 7:04 am

    I have the same problem as max but in German. My files have the day names: Mon, Die, Mit, Don, Fri
    Have anyone any idea, how I can convert the day names?
    Thanks in advance
    Josh

  1. No trackbacks yet.

Leave a comment