Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
$results = foreach ( $DHCPServer in $DHCPServerList ){
if (Test-Connection -ComputerName $DHCPServer -Count 2 -Quiet) {
try {
## Export the DHCP Server Configuration to a Folder ##
$dhcpExportPath = "\\SharedPath\01_XML_Backup\$DHCPServer\$DHCPServer-$(get-date -f yyyy-MM-dd).xml"
#Take a look at splatting
$dhcpExport = @{
ComputerName = $DHCPServer
File = $dhcpExportPath
ErrorAction = "Stop"
}
#Run the command
Export-DhcpServer @dhcpExport
#This will only be run if the command was successful
$exportDHCPStatus = "Success"
try {
## Copy the .XML file to Common location ##
Copy-Item -Path $dhcpExportPath -Destination "E:\Script\DHCP_XML_Backup\02_XML_All\$(get-date -f yyyy-MM-dd)" -ErrorAction Stop
$exportCopyStatus = "Success"
}
catch {
$exportCopyStatus = "Failed. {0}" -f $_
}
}
catch {
$exportDHCPStatus = "Failed. {0}" -f $_
}
}
else {
$exportDHCPStatus = "Ping Failed. Server {0} unreacheable" -f $DHCPServer
}
$props = @{
Server = $DHCPServer
ExportStatus = $exportDHCPStatus
ExportCopy = $exportCopyStatus
}
New-Object -TypeName PSObject -Property $props
}
$EmailBody = @"
<html>
<head>
<title>Report</head>
</head>
<body>
Dear All,
Please find attached DHCP Configuration and HA Backup. Dated :: $(get-date -f yyyy-MM-dd).
$($results | ConvertTo-HTML -Fragment | Out-String)
Regards,
Central Server Operations
</body>
</head>
"@
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment