Effortless Data Assurance: Streamlining DHCP Server Backup with an Automatic Script

Effortless Data Assurance: Streamlining DHCP Server Backup with an Automatic Script
#*****************************************************************
#
#   Script Name:  DHCPServersBackup.ps1
#   Version:  1.0
#   Author:  Umesh Kumar
#   Website: waytoinfotech.com
#   More info: https://www.waytoinfotech.com/dhcpserversbackup
#   Description:  Used to backup DHCP database and logs from the DHCP server
#   to another location for DR purposes
#
#***************************************************************** 
# Change with your backup location
$FolderName = "C:\script\DHCP-Backup\"
# if AllDHCPServers text file is not exist in the folder, it's will be automatically create folder and 
# open folder, please put your input and then save (Servername)
if(-not(Test-path $FolderName\AllDHCPServers.txt))
 {
  New-Item $FolderName\AllDHCPServers.txt
  start $FolderName\AllDHCPServers.txt
 }
else
{
    Write-Host "File Exists"
}

#remote DHCPServers list 
$DHCPServers= Get-Content -Path "$FolderName\AllDHCPServers.txt" 

if (Test-Path $FolderName) {
   
    Write-Host "Folder Exists"
    # Perform Delete file from folder operation
}
else
{
  
    #PowerShell Create directory if not exists
    New-Item $FolderName -ItemType Directory
    Write-Host "Folder Created successfully"
}
$FolderName1 = "$FolderName\Logs"
if (Test-Path $FolderName1) {
   
    Write-Host "Folder Exists"
    # Perform Delete file from folder operation
}
else
{
  
    #PowerShell Create directory if not exists
    New-Item $FolderName1 -ItemType Directory
    Write-Host "Folder Created successfully"
}
Start-Transcript -Path $FolderName1\Transcript.log
foreach ($DHCPServer in $DHCPServers) 

Write-Host "Currently the script is copying files  on" $DHCPServer 
set-location $FolderName
#$path=new-item -type directory $(get-date -f dd-MM-yyyy_HH-mm)
$path = "$FolderName-$DHCPServer$((Get-Date).ToString('dd-MM-yyyy_HH-mm'))"
New-Item -ItemType Directory -Path $path
Backup-DhcpServer -ComputerName $DHCPServer -Path "$path"
#remove older then 15days
Get-ChildItem -Path "$FolderName" -Directory -recurse| where {$_.LastWriteTime -le $(get-date).AddDays(-15)} | Remove-Item -recurse -force
}
Stop-Transcript

Share :

Add New Comment

 Your Comment has been sent successfully. Thank you!   Refresh
Error: Please try again