Powershell: backup all DHCP servers

This script MUST be run on a 2012 server

#you define some share ( eg \server1dhcpbackup$  , and DNS suffix eg my.domain.com

#remove any variables that might have been stored, otherwise it has a bit of a wobble.
Remove-Variable dhcpServer
Remove-Variable dhcpServers

#we are going to do a check first for all files/folders that are older than X days ( you specify in the code )
# set folder path
$folderloc = “\somesharedhcpbackups$”

# set min age of files
$max_days = “70”

# get the current date
$curr_date = Get-Date

# determine how far back we go based on current date
$del_date = $curr_date.AddDays($max_days)

# delete the files
Get-ChildItem $folderloc -Recurse | Where-Object { $_.LastWriteTime -lt $del_date } | Remove-Item -Recurse -Confirm:$False

#this bit finds all the servers ( in the DC !) and only returns the chosen suffx ones as there are ones in the US
$dhcpServers= Get-DhcpServerInDC | where-object {$_.dnsname –match “dns suffix”}

#going to prepare a timestamp system now. l33t style.
$CurrentDate = Get-Date
$CurrentDate = $CurrentDate.ToString(‘dd-MM-yyyy_hh-mm-ss’)

#this bit loops and does the dhcp backup. Backs up the files locally to the path you can see, then copys the file to the $severloc ( which is in
#essence just the dhcpservers dns name )
foreach($dhcpServer in $dhcpServers){
$serverloc = $dhcpServer.DnsName
$from = “\$serverlocc$DHCPBackups”
backup-dhcpserver -ComputerName $dhcpserver.DnsName -Path “c:DHCPBackups”
Copy-Item $from \somesharedhcpbackups$$CurrentDate$serverloc -Recurse }







Leave a Reply

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