Skip to content

Instantly share code, notes, and snippets.

@rasimmers
Created November 16, 2016 22:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rasimmers/e96c177ad24386636ee83ee4fc16e78b to your computer and use it in GitHub Desktop.
Save rasimmers/e96c177ad24386636ee83ee4fc16e78b to your computer and use it in GitHub Desktop.
$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