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.
- 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.
- 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###############################################################