HOWTO: Automated DHCP Backups

I created the script below to back up the DHCP servers in our environment, set it up as a nightly scheduled task and then frankly forgot about it.  That is until a few days ago when due to a power outage, we lost a DHCP server that refused to boot once power was restored.  Thanks to this script, restoring my DHCP server was trivially easy.  Since it so greatly saved the day for me, I wanted to make it available to you as well.

 

To restore from these backups, complete the following few steps:

1) Copy the backup file to the DHCP server you wish to restore to
2) Ensure the DHCP role is installed
3) open an administrative command prompt and run the following command:
netsh dhcp server import [path to file] all

The DHCP scope, configuration and lease information should now be restored.

Oh and I almost forgot to mention the best part.  This script does not require configuration.  Just run it and it’ll automatically go out and find all of the Active Directory authorized DHCP servers in your entire Domain and back them all up automatically to a central store.  (Ok, so you have to define that).  This worked especially well when after I rebuilt my new DHCP server I was surprised to discover it had already been backed up!

 

[snippet id=”12″]

2 thoughts on “HOWTO: Automated DHCP Backups”

  1. I am getting below error can you please please help

    Test-Path : Cannot retrieve the dynamic parameters for the cmdlet. The specified wildcard pattern is not valid: [
    At C:\\San1.ps1:54 char:16
    + if (Test-Path <<<< $BackupDirectory) { } # FOLDER ALREADY EXISTS SO DO NOTHING #
    + CategoryInfo : InvalidArgument: (:) [Test-Path], ParameterBindingException
    + FullyQualifiedErrorId : GetDynamicParametersException,Microsoft.PowerShell.Commands.TestPathCommand

    The term '[C:\PSTOOL\psexec.exe]' is not recognized as the name of a cmdlet, function, script file, or operable prog
    . Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
    At C:\San1.ps1:59 char:3
    + & <<<< $PSexecPath \\$DHCPServer netsh dhcp server export $BackupDirectory\$DayofWeek-$dhcpserver.bak all
    + CategoryInfo : ObjectNotFound: ([C:\PSTOOL\psexec.exe]:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

    Test-Path : Cannot retrieve the dynamic parameters for the cmdlet. The specified wildcard pattern is not valid: [
    At C:\desktop\San1.ps1:54 char:16
    + if (Test-Path <<<< $BackupDirectory) { } # FOLDER ALREADY EXISTS SO DO NOTHING #
    + CategoryInfo : InvalidArgument: (:) [Test-Path], ParameterBindingException
    + FullyQualifiedErrorId : GetDynamicParametersException,Microsoft.PowerShell.Commands.TestPathCommand

    The term '[C:\PSTOOL\psexec.exe]' is not recognized as the name of a cmdlet, function, script file, or operable prog
    . Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
    At C:\San1.ps1:59 char:3
    + & <<<< $PSexecPath \\$DHCPServer netsh dhcp server export $BackupDirectory\$DayofWeek-$dhcpserver.bak all
    + CategoryInfo : ObjectNotFound: ([C:\PSTOOL\psexec.exe]:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

    Test-Path : Cannot retrieve the dynamic parameters for the cmdlet. The specified wildcard pattern is not valid: [
    At C:\Users\San1.ps1:54 char:16
    + if (Test-Path <<<< $BackupDirectory) { } # FOLDER ALREADY EXISTS SO DO NOTHING #
    + CategoryInfo : InvalidArgument: (:) [Test-Path], ParameterBindingException
    + FullyQualifiedErrorId : GetDynamicParametersException,Microsoft.PowerShell.Commands.TestPathCommand

    The term '[C:\PSTOOL\psexec.exe]' is not recognized as the name of a cmdlet, function, script file, or operable prog
    . Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
    At C:\desktop\San1.ps1:59 char:3
    + & <<<< $PSexecPath \\$DHCPServer netsh dhcp server export $BackupDirectory\$DayofWeek-$dhcpserver.bak all
    + CategoryInfo : ObjectNotFound: ([C:\PSTOOL\psexec.exe]:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

    Reply
  2. Sandeep, ensure you run and accept the EULA for the PSEXEC executable before scheduling and remove the [] around your backup path.

    Reply

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.