Solved

Backup DHCP server database using powershell on share location

Posted on 2015-01-14
3
1,303 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

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

A company’s centralized system that manages user data, security, and distributed resources is often a focus of criminal attention. Active Directory (AD) is no exception. In truth, it’s even more likely to be targeted due to the number of companies …
Active Directory security has been a hot topic of late, and for good reason. With 90% of the world’s organization using this system to manage access to all parts of their IT infrastructure, knowing how to protect against threats and keep vulnerabil…
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles from a Windows Server 2008 domain controller to a Windows Server 2012 domain controlle…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

679 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