Want DHCP Backup status in Mailbody or CSV File

I have created a below script to take the backup of the list of DHCP in XML file, then copy them to the common location. Post that ZIP that common location and send it over mail.

I need help from you people to modify this. Below is the modification that we want.

  1. If we can fetch the DHCP Server list from CSV file or .txt file. CSV file or TXT file will have the hostname of the servers.
  2. We should get the status of the backup completion status in email body against each server like below mentioned table.
    DHCP Hostname Backup Status Failover Backup Status
    Server1 Completed Completed

Please help to achive that.

#############################################START OF SCRIPT###############################################################
DHCP_Backup_XML.ps1
All options are set as variables in the GLOBALS section so you simply run the script.

.NOTES

This script requires the DhcpServer module.

The account running the script or scheduled task obviously must have the appropriate permissions on each server.

NAME: DHCP_Backup_XML.ps1

AUTHOR: Avijit Dutta

CREATED: 16-Aug-2016

LASTEDIT: 17-Aug-2016
#>

Create a Common Directory for XML Backup

New-Item -path “E:\Script\DHCP_XML_Backup\02_XML_All$(get-date -f yyyy-MM-dd)” -type directory -Force

List all the DHCP Server in the variable

$DHCPServerList = ‘Server1’,‘server2’,‘server3’,‘server4’

Call Each DHCP Server to export the configuration and HA Config in XML Format

Foreach ( $DHCPServer in $DHCPServerList )
{

Export the DHCP Server Configuration to a Folder

Export-DhcpServer -ComputerName $DHCPServer -File \SharedPath\01_XML_Backup$DHCPServer$DHCPServer-$(get-date -f yyyy-MM-dd).xml -ErrorAction Ignore

Copy the .XML file to Common location

Copy-Item -Path \SharedPath\01_XML_Backup$DHCPServer$DHCPServer-$(get-date -f yyyy-MM-dd).xml -Destination E:\Script\DHCP_XML_Backup\02_XML_All$(get-date -f yyyy-MM-dd)

Export the Failover Configuration to a folder

Get-DhcpServerv4Failover -ComputerName $DHCPServer | Export-Clixml \SharedPath\01_XML_Backup$DHCPServer$DHCPServer-FailoverConf-$(get-date -f yyyy-MM-dd).xml -ErrorAction Ignore

Copy the .XML file to common location

Copy-Item -Path \SharedPath\01_XML_Backup$DHCPServer$DHCPServer-FailoverConf-$(get-date -f yyyy-MM-dd).xml -Destination E:\Script\DHCP_XML_Backup\02_XML_All$(get-date -f yyyy-MM-dd)
}

Below Code is to ZIP the folder

$ZIPSource = “E:\Script\DHCP_XML_Backup\02_XML_All$(get-date -f yyyy-MM-dd)”
$ZIPDestination = “E:\Script\DHCP_XML_Backup\03_ZIP\DHCP_ConfXMLBackup-$(get-date -f yyyy-MM-dd).zip”
If(Test-path $ZIPDestination) {Remove-item $ZIPDestination}
Add-Type -assembly “system.io.compression.filesystem”
[io.compression.zipfile]::CreateFromDirectory($ZIPSource, $ZIPDestination)

Send the ZIP File over Mail as an Attachment

$EmailFrom = “DHCPReport@XYZ.com
$EmailTo = “avijit.dutta@XYZ.com
$EmailCC = “avijit.dutta@XYZ.com
$EmailSubject = “DHCP Configuration and HA Backup - Dated :: $(get-date -f yyyy-MM-dd)”
$EmailBody = @"

Dear All,
Please find attached DHCP Configuration and HA Backup. Dated :: $(get-date -f yyyy-MM-dd).

Regards,
Central Server Operations

"@
$Attachment = $ZIPDestination
$SMTPServer = “SMTP-relay.XYZ.com
$SMTPPort = “25”

$Message = New-Object System.Net.Mail.MailMessage
$Message.From = $EmailFrom
$Message.To.Add($EmailTo)
$Message.CC.Add($EmailCC)
$Message.Body = $EmailBody
$Message.Subject = $EmailSubject
$Attach = New-Object Net.Mail.Attachment($Attachment)
$Message.Attachments.Add($attach)
$Message.IsBodyHTML = $true
$SMTPClient = New-Object Net.Mail.SmtpClient($SMTPServer, $SMTPPort)
$SMTPClient.Send($Message)
$Message.Dispose()

#############################################START OF SCRIPT###############################################################

Your script is a little hard to follow because you didn’t have it formatted as code. Those instructions are posted just above the text box here on the site.

But you do appear to be constructing an email with attachments. What is not working for you?

Hello Don… The above script currently takes the DHCP Configuration backup in a folder, then ZIP that folder and send it over mail.

But with current script, I am not able to check whether Backup is completed or not. If we can some output over mail body in HTML table format to get Hostname and status of the Configuration backup and Failover Backup Like Completed or Failed or Server not reachable.

This will really help.

DHCP_Backup_XML.ps1
All options are set as variables in the GLOBALS section so you simply run the script.

.NOTES

This script requires the DhcpServer module.

The account running the script or scheduled task obviously must have the appropriate permissions on each server.

NAME: DHCP_Backup_XML.ps1

AUTHOR: Avijit Dutta

CREATED: 16-Aug-2016

LASTEDIT: 17-Aug-2016
#>

## Create a Common Directory for XML Backup ##

New-Item -path "E:\Script\DHCP_XML_Backup\02_XML_All\$(get-date -f yyyy-MM-dd)" -type directory -Force

## List all the DHCP Server in the variable ##

$DHCPServerList = 'Server1','server2','server3','server4'

## Call Each DHCP Server to export the configuration and HA Config in XML Format

Foreach ( $DHCPServer in $DHCPServerList )
{
## Export the DHCP Server Configuration to a Folder ##
Export-DhcpServer -ComputerName $DHCPServer -File \\SharedPath\01_XML_Backup\$DHCPServer\$DHCPServer-$(get-date -f yyyy-MM-dd).xml -ErrorAction Ignore

## Copy the .XML file to Common location ##
Copy-Item -Path \\SharedPath\01_XML_Backup\$DHCPServer\$DHCPServer-$(get-date -f yyyy-MM-dd).xml -Destination E:\Script\DHCP_XML_Backup\02_XML_All\$(get-date -f yyyy-MM-dd)

## Export the Failover Configuration to a folder ##
Get-DhcpServerv4Failover -ComputerName $DHCPServer | Export-Clixml \\SharedPath\01_XML_Backup\$DHCPServer\$DHCPServer-FailoverConf-$(get-date -f yyyy-MM-dd).xml -ErrorAction Ignore

## Copy the .XML file to common location ##
Copy-Item -Path \\SharedPath\01_XML_Backup\$DHCPServer\$DHCPServer-FailoverConf-$(get-date -f yyyy-MM-dd).xml -Destination E:\Script\DHCP_XML_Backup\02_XML_All\$(get-date -f yyyy-MM-dd)
}

## Below Code is to ZIP the folder ##
$ZIPSource = "E:\Script\DHCP_XML_Backup\02_XML_All\$(get-date -f yyyy-MM-dd)"
$ZIPDestination = "E:\Script\DHCP_XML_Backup\03_ZIP\DHCP_ConfXMLBackup-$(get-date -f yyyy-MM-dd).zip"
If(Test-path $ZIPDestination) {Remove-item $ZIPDestination}
Add-Type -assembly "system.io.compression.filesystem"
[io.compression.zipfile]::CreateFromDirectory($ZIPSource, $ZIPDestination)

## Send the ZIP File over Mail as an Attachment ##

$EmailFrom = "DHCPReport@XYZ.com"
$EmailTo = "avijit.dutta@XYZ.com"
$EmailCC = "avijit.dutta@XYZ.com"
$EmailSubject = "DHCP Configuration and HA Backup – Dated :: $(get-date -f yyyy-MM-dd)"
$EmailBody = @"

Dear All,
Please find attached DHCP Configuration and HA Backup. Dated :: $(get-date -f yyyy-MM-dd).

Regards,
Central Server Operations

"@
$Attachment = $ZIPDestination
$SMTPServer = "SMTP-relay.XYZ.com"
$SMTPPort = "25"

$Message = New-Object System.Net.Mail.MailMessage
$Message.From = $EmailFrom
$Message.To.Add($EmailTo)
$Message.CC.Add($EmailCC)
$Message.Body = $EmailBody
$Message.Subject = $EmailSubject
$Attach = New-Object Net.Mail.Attachment($Attachment)
$Message.Attachments.Add($attach)
$Message.IsBodyHTML = $true
$SMTPClient = New-Object Net.Mail.SmtpClient($SMTPServer, $SMTPPort)
$SMTPClient.Send($Message)
$Message.Dispose()

You can try something like below. There are a couple of ebooks in the link above that could help such as Big Book of Powershell Error Handling and Creating HTML Reports in PowerShell. The try\catch statements are nested, which means that the previous command has to succeed before the next command will run. This should get you close to what you are wanting: