POWERSHELL SCRIPT:: DHCP BACKUP SCRIPT WITH NOTIFICATION


This script can be scheduled from a remote machine to execute across DHCP servers

Below command to find list of Authorized DHCP servers,

(Get-ADObject -SearchBase “cn=configuration,dc=domain,dc=net” -Filter “objectclass -eq ‘dhcpclass’ -AND Name -ne ‘dhcproot'”).name, this can be incorported instead of  [array]$lists = @(“DHCP01″,”DHCP02″,”DHCP03”). Like [array]$lists = (Get-ADObject -SearchBase “cn=configuration,dc=domain,dc=net” -Filter “objectclass -eq ‘dhcpclass’ -AND Name -ne ‘dhcproot'”).name

Below script to run DHCP Database backup

#———————————– Script Start———————————————————-

$body =

<p><u><span style=’font-size:10.0pt;font-family:Century Gothic’>This is an automatically generated message from DHCP server</span></u> </p>

<p style=’font-family:Century Gothic’>Dear Team,</p>

<p><span style=’font-size:10.0pt;font-family:Century Gothic’>Please find DHCP backup status on remote DCs</span></p>

$old = (get-date).AddDays(-10)

[array]$lists = @(“DHCP01″,”DHCP02″,”DHCP03”)
foreach ($list in $lists){
$date =get-date -format MMddyyyy
$server = $list
Get-ChildItem \\Backup_Server\d$\DHCP\$server | ?{$_.CreationTime -le $old} |Remove-Item -Force
$Error.Clear()
$filename = “\\Backup_Server\d$\DHCP\$server\”+$server+”-“+$date+”.xml”
Try
{
Export-dhcpserver -computername $server -leases -file $filename -ver
}

catch
{
“DHCP Export failed”
}
Finally
{
if ($error[0] -eq $null)
{
$Time=Get-Date
$body += “<p><span style=’font-size:10.0pt;font-family:Century Gothic’>DHCP Export backup of server $server completed at $Time and stored on to $filename </span></p>”
}
else
{
$body += “<p><span style=’font-size:10.0pt;font-family:Century Gothic’>DHCP Export backup of server $server <b><span style=’color:red’>failed </b></span> to complete with error $error</span></p> ”
}

}
}
$body += ”

<p><span style=’font-size:10.0pt;font-family:Century Gothic’>Thank you, </span></p>

<p><span style=’font-size:10.0pt;font-family:Century Gothic’>Windows Operations Team </span></p>”

$recipients =”OperationsTeam@domain.com”
send-mailmessage -from “OperationsTeam@domain.com” -to $recipients -subject “Update::DHCP server database backup” -body $Body -smtpServer smtp.domain.com -BodyAsHtml

#———————————– Script End———————————————————-

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s