DHCP: Troubleshooting

I got a weird case that DCHP settings has disappeared. Scope options and lease times where gone and resett, some routes where gone as well. We did use powershell to find effected scopes and to restore some of the options.

The code is not pretty but it does the job for

$DHCPServer = "DHCPSRV001"
$DNSServer = "192.168.1.1","192.168.1.2"
$LeaseDuration = "04:00:00"

Write-Host "Collecting DHCP Scope" -ForegroundColor Green
$scope = Get-DhcpServerv4Scope -ComputerName $DHCPServer
$scope.count

Write-Host "Collecting DHCP Scope with empty lease time" -ForegroundColor Green
$noleasetime = $scope.where{$_.LeaseDuration -like "00:00:00"}
$noleasetime.Count

Write-Host "Set DHCP lease time to 4 hour" -ForegroundColor Yellow
$noleasetime | Set-DhcpServerv4Scope -LeaseDuration $LeaseDuration -ComputerName $DHCPServer -WhatIf

Write-Host "Collecting DHCP Scope with empty DNS server option" -ForegroundColor Green
$scopeMissingDNS = foreach ($s in $scope){
    $sOption = $s | Get-DhcpServerv4OptionValue -ComputerName $DHCPServer
    if ($sOption.OptionId -notcontains "6"){
        [pscustomobject]@{
        ScopeID = $s.ScopeId
        }
    }
}
$scopeMissingDNS.Count

Write-Host "Set DHCP Scope with DNS server option $DNSServer" -ForegroundColor yellow
$scopeMissingDNS | Set-DhcpServerv4OptionValue -OptionId 6 -Value $DNSServer -ComputerName $DHCPServer -WhatIf

Write-Host "Collecting DHCP Scope with empty Route" -ForegroundColor Green
$scopeMissingRoute = foreach ($s in $scope){
    $sOption = $s | Get-DhcpServerv4OptionValue -ComputerName $DHCPServer
    if ($sOption.OptionId -notcontains "3"){
        [pscustomobject]@{
        ScopeID = $s.ScopeId
        }
    }
}
$scopeMissingRoute.Count

Leave a Reply

Your email address will not be published.