Backup DHCP logs from the DHCP server

A script to save DHCP logfiles more than One Week on a Windows server 2008 R2.  It will also delete files older than XX days and Dump the DHCP database to a text-file in the logfolder.
Schedule the script to run once a day.



#**************************************************************
#
#   Script Name:  dhcpBackup.ps1
#   Version:  1.0
#   Author:  Per Wiberg, Headworks Consulting
#
#   Description:  Used to backup DHCP logs from the DHCP server
#   to another location for archiving purposes.
#   Deletes logfiles that´s older than 30 Days
#
#*****************************************************************

# Parameters
$DHCPBackupPath = "C:\DHCP-LogFiles-Path"
$DHCPServerName = "\\Server1"
$DHCPPath = "C:\Windows\System32\dhcp"
$Path = C:\DHCP-LogFiles-Path"
$Daysback = "-30"

#Remove Files older than the $limit. Remove "Whatif" to actiwait
$CurrentDate = Get-Date
$DatetoDelete = $CurrentDate.AddDays($Daysback)
Get-ChildItem $Path | Where-Object { $_.LastWriteTime -lt $DatetoDelete } | Remove-Item -Whatif

#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 $DHCPPath
copy "DhcpSrvLog-$logdate.log" $DHCPBackupPath

#Rename log file with yesterdays date
cd $DHCPBackupPath
rename-item "DhcpSrvLog-$logdate.log" "$yesterday.log"

#Dump DHCP database
$today=(get-date -uformat %Y%m%d)
$dumpfile="DHCP_DUMP-$today.txt"
$dumpfilePath = $DHCPBackupPath + "\" + $dumpfile
netsh dhcp server $DHCPServerName dump > $dumpfilePath
#*****************************************************************


Comments

  1. i got "the string is missing the terminator:" in line 42 - please help

    ReplyDelete

Post a Comment

Popular Posts