Solved

Backup DHCP server database using powershell on share location

Posted on 2015-01-14
3
1,227 Views
Last Modified: 2015-01-19
Hello,

is anybody has powershell script for backing up DHCP server on the share location?

i tried netsh command which used for backing up DHCP server but it stored all database files on the same server.

thanks in advance
takecofffe
0
Comment
Question by:Haresh Nikumbh
3 Comments
 
LVL 14

Accepted Solution

by:
JAN PAKULA earned 250 total points
ID: 40549548
Use code from

http://poshcode.org/4017


Discovers DHCP online servers and if SystemRoot\Windows\System32\DHCP\Backup exists, copies the folder on each server to a network share (\\network\share\hostname).

Accepts two parameters:
SearchBase – Location of domain’s configuration container.
BackupDestRoot – Base folder where a folder for each host will be created to contain the copied DHCP database backups

Usage:
.\Backup-DhcpServers.ps1 -SearchBase “cn=configuration,dc=domain,dc=com” -BackupDestRoot “\\network\share\”

Two functions:
Get-OnlineDhcpServers
Backup-DhcpServers

No comment-based help.
No logging.
No error handling.

Known Issues:
BackupDestRoot parameter MUST end with a backslash (\). The script will eventually add a DHCP server’s hostname to this path to create that server’s backup destination.


###############################################
Import-Module ActiveDirectory
 
param (
    [Parameter(Position=1)]
    $searchBase = "cn=configuration,dc=domain,dc=com",
    [Parameter(Position=2)]
    $backupDestRoot = "\\network\share\"
)
 
function Get-OnlineDhcpServers {
    param (
        [Parameter(Mandatory=$true,Position=1)]
        $dhcpSearchBase
        )
    $arrDhcpServers = Get-ADObject -SearchBase $dhcpSearchBase -Filter "objectclass -eq 'dhcpclass' -AND name -ne 'dhcproot'"
    ForEach ($dhcpServer in $arrDhcpServers) {
        if (!(Test-Connection -ComputerName $dhcpServer.name -Count 2 -Quiet -ErrorAction SilentlyContinue)) {
            $arrDhcpServers = @($arrDhcpServers | Where-Object {$_.name -ne $dhcpServer.name})
        }
    }
    return $arrDhcpServers
}
 
function Backup-DhcpServers {
    param (
        [Parameter(Mandatory=$true,Position=1)]
        $arrDhcpBackupSrcNames,
        [Parameter(Mandatory=$true,Position=2)]
        $dhcpBackupDestRoot
    )
    ForEach ($dhcpBackupSrcName in $arrDhcpBackupSrcNames) {
        $dhcpBackupSrc = "\\" + $dhcpBackupSrcName + "\c$\Windows\System32\Dhcp\Backup"
        $dhcpBackupDest = $dhcpBackupDestRoot + $dhcpBackupSrcName
        if (Test-Path -Path $dhcpBackupSrc) {
            Remove-Item -Path $dhcpBackupDest -Recurse -Force -Verbose
            New-Item -Path $dhcpBackupDest -ItemType Directory
            Copy-Item -Path $dhcpBackupSrc -Destination $dhcpBackupDest -Recurse -Verbose
        }
    }
}
 
$onlineDhcpServers = Get-OnlineDhcpServers -dhcpSearchBase $searchBase
$onlineDhcpServerNames = $onlineDhcpServers | ForEach-Object {$_.name.Split(".")[0]}
Backup-DhcpServers -arrDhcpBackupSrcNames $onlineDhcpServerNames -dhcpBackupDestRoot $backupDestRoot

#############################################
0
 
LVL 53

Assisted Solution

by:Will Szymkowski
Will Szymkowski earned 250 total points
ID: 40549568
If you do not use server 2012 then you cannot use the new DHCP cmdlets. However the script center has created a nice easy script that will export DHCP config for your servers. Take a look at the below link where you can download this from the Microsoft Script Center.

Export DHCP Settings (Microsoft Script Center)

Will.
0
 
LVL 22

Author Closing Comment

by:Haresh Nikumbh
ID: 40558073
Thanks
0

Featured Post

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

OfficeMate Freezes on login or does not load after login credentials are input.
A brief introduction to what I consider to be the best editor for PowerShell.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

823 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question