DHCP Toolkit
<# .SYNOPSIS DHCP Toolkit .DESCRIPTION This script provides comprehensive analysis and management options for DHCP servers in a Windows environment. .NOTES File Name : DHCPToolkit.ps1 Author : [Your Name] Prerequisite : PowerShell V5.1 or later, DHCP Server module, and appropriate admin rights Version : 1.0 Date : [Current Date] .EXAMPLE .\DHCPToolkit.ps1 #> # Import required module Import-Module DHCPServer # Global variables $global:reportPath = "$env:USERPROFILE\Desktop\DHCP_Analysis_Report_$(Get-Date -Format 'yyyyMMdd_HHmmss').html" $global:dhcpServer = $env:COMPUTERNAME # Default to local machine function Show-Menu { Clear-Host Write-Host "=== DHCP Toolkit ===" -ForegroundColor Cyan Write-Host "Current DHCP Server: $global:dhcpServer" Write-Host "1. Set DHCP Server" Write-Host "2. Get DHCP Server Information" Write-Host "3. List DHCP Scopes" Write-Host "4. Analyze Scope Utilization" Write-Host "5. Check DHCP Options" Write-Host "6. List DHCP Reservations" Write-Host "7. Check DHCP Failover Configuration" Write-Host "8. Analyze DHCP Lease History" Write-Host "9. Check DHCP Server Statistics" Write-Host "10. Generate Comprehensive HTML Report" Write-Host "11. Exit" } function Set-DHCPServer { $server = Read-Host "Enter the DHCP server name (or press Enter for localhost)" if ([string]::IsNullOrWhiteSpace($server)) { $global:dhcpServer = $env:COMPUTERNAME } else { $global:dhcpServer = $server } Write-Host "DHCP server set to: $global:dhcpServer" -ForegroundColor Green } function Get-DHCPServerInformation { Write-Host "`nGathering DHCP Server Information..." -ForegroundColor Yellow try { $serverInfo = Get-DhcpServerSetting -ComputerName $global:dhcpServer $result = [PSCustomObject]@{ ServerName = $global:dhcpServer ConflictDetectionAttempts = $serverInfo.ConflictDetectionAttempts DynamicBootp = $serverInfo.DynamicBootp IsAuthorized = $serverInfo.IsAuthorized NapEnabled = $serverInfo.NapEnabled RestoreStatus = $serverInfo.RestoreStatus } $result | Format-List return $result } catch { Write-Host "Error getting DHCP server information: $_" -ForegroundColor Red return $null } } function Get-DHCPScopes { Write-Host "`nListing DHCP Scopes..." -ForegroundColor Yellow try { $scopes = Get-DhcpServerv4Scope -ComputerName $global:dhcpServer $scopes | Format-Table -AutoSize return $scopes } catch { Write-Host "Error getting DHCP scopes: $_" -ForegroundColor Red return $null } } function Analyze-ScopeUtilization { Write-Host "`nAnalyzing Scope Utilization..." -ForegroundColor Yellow try { $scopes = Get-DhcpServerv4Scope -ComputerName $global:dhcpServer $results = @() foreach ($scope in $scopes) { $stats = Get-DhcpServerv4ScopeStatistics -ComputerName $global:dhcpServer -ScopeId $scope.ScopeId $results += [PSCustomObject]@{ ScopeName = $scope.Name ScopeId = $scope.ScopeId TotalAddresses = $stats.TotalAddresses InUse = $stats.AddressesInUse Available = $stats.AddressesAvailable UtilizationPercentage = [math]::Round(($stats.AddressesInUse / $stats.TotalAddresses) * 100, 2) } } $results | Format-Table -AutoSize return $results } catch { Write-Host "Error analyzing scope utilization: $_" -ForegroundColor Red return $null } } function Check-DHCPOptions { Write-Host "`nChecking DHCP Options..." -ForegroundColor Yellow try { $serverOptions = Get-DhcpServerv4OptionValue -ComputerName $global:dhcpServer $scopeOptions = Get-DhcpServerv4Scope -ComputerName $global:dhcpServer | ForEach-Object { Get-DhcpServerv4OptionValue -ComputerName $global:dhcpServer -ScopeId $_.ScopeId } $results = @{ ServerOptions = $serverOptions ScopeOptions = $scopeOptions } $serverOptions | Format-Table -AutoSize Write-Host "Scope-specific options are available in the returned object." -ForegroundColor Yellow return $results } catch { Write-Host "Error checking DHCP options: $_" -ForegroundColor Red return $null } } function List-DHCPReservations { Write-Host "`nListing DHCP Reservations..." -ForegroundColor Yellow try { $scopes = Get-DhcpServerv4Scope -ComputerName $global:dhcpServer $results = @() foreach ($scope in $scopes) { $reservations = Get-DhcpServerv4Reservation -ComputerName $global:dhcpServer -ScopeId $scope.ScopeId foreach ($reservation in $reservations) { $results += [PSCustomObject]@{ ScopeId = $scope.ScopeId IPAddress = $reservation.IPAddress ClientId = $reservation.ClientId Name = $reservation.Name } } } $results | Format-Table -AutoSize return $results } catch { Write-Host "Error listing DHCP reservations: $_" -ForegroundColor Red return $null } } function Check-DHCPFailover { Write-Host "`nChecking DHCP Failover Configuration..." -ForegroundColor Yellow try { $failoverRelationships = Get-DhcpServerv4Failover -ComputerName $global:dhcpServer if ($failoverRelationships) { $failoverRelationships | Format-Table -AutoSize } else { Write-Host "No failover relationships configured." -ForegroundColor Yellow } return $failoverRelationships } catch { Write-Host "Error checking DHCP failover configuration: $_" -ForegroundColor Red return $null } } function Analyze-DHCPLeaseHistory { Write-Host "`nAnalyzing DHCP Lease History..." -ForegroundColor Yellow try { $startDate = (Get-Date).AddDays(-7) $leaseHistory = Get-DhcpServerv4ScopeStatistics -ComputerName $global:dhcpServer | ForEach-Object { Get-DhcpServerv4LeaseHistory -ComputerName $global:dhcpServer -ScopeId $_.ScopeId -StartDate $startDate } $results = $leaseHistory | Group-Object ScopeId | ForEach-Object { [PSCustomObject]@{ ScopeId = $_.Name LeaseCount = $_.Count UniqueClients = ($_.Group | Select-Object -ExpandProperty ClientId -Unique).Count } } $results | Format-Table -AutoSize return $results } catch { Write-Host "Error analyzing DHCP lease history: $_" -ForegroundColor Red return $null } } function Check-DHCPServerStatistics { Write-Host "`nChecking DHCP Server Statistics..." -ForegroundColor Yellow try { $stats = Get-DhcpServerStatistics -ComputerName $global:dhcpServer $result = [PSCustomObject]@{ TotalScopes = $stats.TotalScopes TotalAddresses = $stats.TotalAddresses AddressesInUse = $stats.AddressesInUse AddressesAvailable = $stats.AddressesAvailable PercentageInUse = [math]::Round(($stats.AddressesInUse / $stats.TotalAddresses) * 100, 2) Discovers = $stats.Discovers Offers = $stats.Offers Requests = $stats.Requests Acks = $stats.Acks Naks = $stats.Naks Declines = $stats.Declines Releases = $stats.Releases } $result | Format-List return $result } catch { Write-Host "Error checking DHCP server statistics: $_" -ForegroundColor Red return $null } } function Generate-HTMLReport { param([hashtable]$AllResults) Write-Host "`nGenerating Comprehensive HTML Report..." -ForegroundColor Yellow $reportContent = @" <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>DHCP Server Analysis Report</title> <style> body { font-family: Arial, sans-serif; line-height: 1.6; color: #333; max-width: 1200px; margin: 0 auto; padding: 20px; } h1, h2, h3 { color: #0078D4; } table { border-collapse: collapse; width: 100%; margin-bottom: 20px; } th, td { border: 1px solid #ddd; padding: 8px; text-align: left; } th { background-color: #f2f2f2; } .warning { color: orange; } .critical { color: red; } </style> </head> <body> <h1>DHCP Server Analysis Report</h1> <p>Generated on: $(Get-Date)</p> <p>DHCP Server: $global:dhcpServer</p> <h2>DHCP Server Information</h2> $($AllResults.ServerInfo | ConvertTo-Html -Fragment) <h2>DHCP Scopes</h2> $($AllResults.Scopes | ConvertTo-Html -Fragment) <h2>Scope Utilization</h2> $($AllResults.ScopeUtilization | ConvertTo-Html -Fragment) <h2>DHCP Options</h2> <h3>Server Options</h3> $($AllResults.DHCPOptions.ServerOptions | ConvertTo-Html -Fragment) <h2>DHCP Reservations</h2> $($AllResults.Reservations | ConvertTo-Html -Fragment) <h2>DHCP Failover Configuration</h2> $($AllResults.FailoverConfig | ConvertTo-Html -Fragment) <h2>DHCP Lease History (Last 7 Days)</h2> $($AllResults.LeaseHistory | ConvertTo-Html -Fragment) <h2>DHCP Server Statistics</h2> $($AllResults.ServerStats | ConvertTo-Html -Fragment) </body> </html> "@ $reportContent | Out-File -FilePath $global:reportPath Write-Host "Report generated and saved to: $global:reportPath" -ForegroundColor Green } # Main program loop $allResults = @{} do { Show-Menu $choice = Read-Host "`nEnter your choice (1-11)" switch ($choice) { "1" { Set-DHCPServer } "2" { $allResults.ServerInfo = Get-DHCPServerInformation } "3" { $allResults.Scopes = Get-DHCPScopes } "4" { $allResults.ScopeUtilization = Analyze-ScopeUtilization } "5" { $allResults.DHCPOptions = Check-DHCPOptions } "6" { $allResults.Reservations = List-DHCPReservations } "7" { $allResults.FailoverConfig = Check-DHCPFailover } "8" { $allResults.LeaseHistory = Analyze-DHCPLeaseHistory } "9" { $allResults.ServerStats = Check-DHCPServerStatistics } "10" { Generate-HTMLReport -AllResults $allResults } "11" { Write-Host "Exiting program..." -ForegroundColor Yellow; break } default { Write-Host "Invalid choice. Please try again." -ForegroundColor Red } } if ($choice -ne "11") { Read-Host "`nPress Enter to continue..." } } while ($choice -ne "11")
This DHCP Toolkit includes:
- A menu-driven interface for easy navigation.
- Functions to analyze and manage various aspects of DHCP servers:
- DHCP Server Information
- DHCP Scopes Listing
- Scope Utilization Analysis
- DHCP Options Check
- DHCP Reservations Listing
- DHCP Failover Configuration Check
- DHCP Lease History Analysis
- DHCP Server Statistics Check
- Option to set a target DHCP server (local or remote)
- HTML report generation for easy sharing and viewing of results
Key features:
- Comprehensive DHCP server information gathering
- Detailed analysis of scope utilization
- Review of DHCP options at server and scope level
- Listing of DHCP reservations across all scopes
- Failover configuration check
- Analysis of recent lease history
- Overview of DHCP server statistics
This tool is particularly useful for:
- Network administrators managing DHCP servers
- IT professionals troubleshooting DHCP-related issues
- System administrators performing regular DHCP health checks
- Anyone needing to quickly gather comprehensive information about DHCP server configurations
To use this script effectively:
- Run PowerShell as an administrator
- Ensure you have the DHCP Server PowerShell module installed (typically available on DHCP servers or management workstations)
- Have the necessary permissions to query DHCP server information (local admin rights on the DHCP server or appropriate delegated permissions)
- Review the generated HTML report for a comprehensive overview of the DHCP server’s configuration and status
This script provides a thorough analysis of a DHCP server, helping to identify potential issues, misconfigurations, or areas that need attention. It’s designed to give administrators a quick but comprehensive view of their DHCP server’s health and configuration.
Leave a Reply
Want to join the discussion?Feel free to contribute!