RedPlug's Tory

Windows DHCP Server의 경우 일단위로 Log 파일이 생성되며, 요일단위로 순환로깅이 적용되어 있습니다.

일단위로 생성되는 로그 파일을 장기간 보관(하기 설정은 1년간 보관)을 위하여  만든(짜집기+수정) 스크립트이며,

해당 스크립트를 일단위로 예약작업 걸어주시면 되겠습니다.

로그 생성하는 부분까지는 Powershell 2.0에서도 정상적으로 작동 합니다만

삭제 하는 부분은 where을 정상적으로 인식하지 못해서 작동하지 않으니 참고하시면 되겠습니다.

감사합니다.

dhcplogbackup.zip


DHCP Log Backup Script

 
### It is only available in PowerShell 3.0 or later ###

## DHCP Log Remote Backup ##

#----- define parameters -----#

#----- 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)

#----- Remote Backup Location -----#
$BackupLocation="백업 위치 지정"

#----- Change path to DHCP log folder, copy yesterdays log file to backup location -----#
cd C:\Windows\System32\dhcp
copy "DhcpSrvLog-$logdate.log" $BackupLocation
 
#----- Rename log file with yesterdays date -----#
cd $BackupLocation
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 dump > $BackupLocation$dumpfile

## DHCP Old Log Delete ##

#----- define parameters -----#

#----- get current date ----#
$Now = Get-Date

#----- define amount of days ----#
$Days = "365"

#----- define extension ----#
$Extension = "*.*"

#----- define LastWriteTime parameter based on $Days ---#
$LastWrite = $Now.AddDays(-$Days)

#----- get files based on lastwrite filter and specified folder ---#
$Files = Get-Childitem $BackupLocation -Include $Extension -Recurse | Where {$_.LastWriteTime -le "$LastWrite"}

foreach ($File in $Files)
    {
    if ($File -ne $NULL)
        {
        write-host "Deleting File $File" -ForegroundColor "DarkRed"
        Remove-Item $File.FullName | out-null
        }
    else
        {
        Write-Host "No more files to delete!" -foregroundcolor "Green"
        }
    }