DHCP export from all Authorized DHCP servers in Domain

This script exports all scope, lease, reservation, scope-options, filters etc information from all DHCP servers to an SMB share. Files older than 30 days are automatically deleted. For importing anything on any DHCP server in future, please read the usage of import-dhcpserver cmdlet usage at https://docs.microsoft.com/en-us/powershell/module/dhcpserver/Import-DhcpServer?view=win10-ps

$old = (get-date).AddDays(-30)
[array]$DHCPServers = (Get-ADObject -SearchBase “cn=configuration,dc=Domain,dc=net” -Filter “objectclass -eq 'dhcpclass' -AND Name -ne 'DhcpRoot').name
foreach ($DHCPServer in $DHCPServers){
$date =get-date -format MMddyyyy
$server = $DHCPServer
Get-ChildItem "\Server\Backups\DHCPBackups\$server" | ?{$_.CreationTime -le $old} |Remove-Item -Force
$filename = "\Server\Backups\DHCPBackups\$server\”+$server+”-“+$date+”.xml”
Export-dhcpserver -computername $server -file $filename

Be the first to comment

Leave a Reply

Your email address will not be published.