Skip to content

Instantly share code, notes, and snippets.

@norman-bauer
Created October 29, 2020 09:09
Show Gist options
  • Select an option

  • Save norman-bauer/b41ebb6734865cd4c0415a6e1bcca33a to your computer and use it in GitHub Desktop.

Select an option

Save norman-bauer/b41ebb6734865cd4c0415a6e1bcca33a to your computer and use it in GitHub Desktop.
This script updates all scopes of all authorized DHCP servers in a domain to use specified dns update credentials, to always update dns records and to set maximum lease duration
$dhcpServers = Get-DhcpServerInDC
$Credential = Get-Credential
$days = Read-Host "Maximum lease duration in days (scopes with longer periods are set to this value)"
$timespan = New-TimeSpan -Days $days
foreach ($dhcpServer in $dhcpServers) {
Write-Host $dhcpServer.DnsName
Set-DhcpServerDnsCredential -Credential $Credential -ComputerName $dhcpServer.DnsName
Write-Host " DNS Update credentials set"
$scopes = Get-DhcpServerv4Scope -ComputerName $dhcpServer.DnsName
foreach ($scope in $scopes) {
Write-Host " $($scope.ScopeId) $($scope.LeaseDuration)"
Set-DhcpServerv4DnsSetting -ComputerName $dhcpServer.DnsName -ScopeId $scope.ScopeId -DynamicUpdates "Always" -UpdateDnsRRForOlderClients $True -DeleteDnsRRonLeaseExpiry $True
if ($scope.LeaseDuration -gt $timespan){
Set-DhcpServerv4Scope -ComputerName $dhcpServer.DnsName -ScopeId $scope.ScopeId -LeaseDuration $timespan
Write-Host " lease duration updated to $timespan"
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment