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
DHCP, PowerShell
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
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?
$logdate=(get-Date (get-date).AddDays(-1) -format dddd).Substring(0,3)
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.
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”
I solved it:
copy “DhcpSrvLog-$logdate.log” \\SERVER\e$\LogRepos\DHCP\$computer\””$computer”_”$yesterday”.log”
Nice. Thanks for the follow-up.
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
Did you change \\DC to the name of your DHCP server?
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