I have the below script on my local machine to clear caches on all DNS in AD. I would like to offer that to my collegues hovever I cannot run the script in PSU 5.2.0
[CmdletBinding(DefaultParametersetName='Forest')]
param (
[Parameter(Mandatory=$false, ParameterSetName="Forest")]
[Parameter(ParameterSetName="Domain")]
[Parameter(ParameterSetName="Server")]
[ValidateSet("Server", "Domain", "Forest")]
[string]$Scope = "Forest",
[Parameter(Mandatory=$true, ParameterSetName="Server")]
[ValidateScript({
try {
# Extract the domain from the FQDN
$InParameterServerName = $_
if ($InParameterServerName -notmatch "(?=^.{4,253}$)(^((?!-)[a-zA-Z0-9-]{1,63}(?<!-)\.)+[a-zA-Z]{2,63}$)") {
throw "Invalid server name: $InParameterServerName. Server name must be a valid FQDN."
}
$ServerNameToValidateDomain = $InParameterServerName -replace "^[^.]+\.", ""
$ServerNameToValidate = $InParameterServerName #-replace "\..*$", ""
# Validate the domain exists
$validDomains = (Get-AdForest).Domains
if ($validDomains -notcontains $ServerNameToValidateDomain) {
throw "Domain '$ServerNameToValidateDomain' does not exist. Make sure you entered a valid server Fully Qualified Domain Name (FQDN)"
}
# Validate the server exists in the domain
$AllDOmainController = Get-ADDomainController -Server $ServerNameToValidateDomain -Filter * | Select-Object -ExpandProperty HostName
if ($AllDOmainController -notcontains $ServerNameToValidate) {
Throw "Server '$ServerNameToValidate' does not exist in domain '$ServerNameToValidateDomain'."
}
$true
} catch {
throw $_.Exception.Message
}
})]
[string]$ServerName, #Change to ADDNSServerName
[Parameter(Mandatory=$true, ParameterSetName="Domain")]
[ValidateScript({
$validDomains = (Get-AdForest).Domains
if ($validDomains -contains $_) {
$true
} else {
throw "Invalid domain: $_. Valid domains are: $($validDomains -join ', ')"
}
})]
[string]$DomainName
)
function Validate-Parameters {
param (
[string]$ServerName,
[string]$DomainName,
[String]$Forest
)
switch ($Scope) {
"Forest" {
if (-not $Forest) {
#throw "Forest is mandatory when Scope is 'Forest'."
}
}
"Domain" {
if (-not $DomainName) {
throw "DomainName is mandatory when Scope is 'Domain'."
}
}
"Server" {
if (-not $ServerName) {
throw "ServerName is mandatory when Scope is 'Server'."
}
}
}
}
try {
Validate-Parameters -ServerName $ServerName -DomainName $DomainName -Forest $Forest
Write-Output "Parameters are valid. Proceeding with scope: $($PSCmdlet.ParameterSetName)"
# Add your code logic here based on the scope
$Scope = $($PSCmdlet.ParameterSetName)
if ($Scope -eq "Server") {
$DomainName = $ServerName -replace "^[^.]+\.", ""
$Dcs = Get-ADDomainController -Server $DomainName -Identity $ServerName | Select-Object -ExpandProperty HostName
} elseif ($Scope -eq "Domain") {
$Dcs = Get-ADDomainController -server $DomainName -Filter * | Select-Object -ExpandProperty HostName
} elseif ($Scope -eq "Forest") {
$Dcs = Get-ADForest | Select-Object -ExpandProperty Domains | ForEach-Object {
(Get-ADDomain $PSItem).ReplicaDirectoryServers
}
}
foreach ($dc in $Dcs) {
$dc
Clear-DnsServerCache -ComputerName $dc -Force -Verbose
}
} catch {
Write-Error $_.Exception.Message
}