PoshCode Archive  Artifact [94b5860b02]

Artifact 94b5860b02a98cde47eb90ea4cd538fb7a5933783b44e80297ffa7355cdecd2a:

  • File DHCP-Backup.ps1 — part of check-in [63ad5548fc] at 2018-06-10 13:34:42 on branch trunk — 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). (user: R Derickson size: 2869)

# encoding: ascii
# api: powershell
# title: DHCP Backup
# description: 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).
# version: 0.1
# type: module
# author: R Derickson
# license: CC0
# function: Get-OnlineDhcpServers
# x-poshcode-id: 4017
# x-archived: 2017-05-22T01:35:38
# x-published: 2013-03-15T14:40:00
#
# 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