Powershell stop and restart multiple KDC

Powershell stop and restart multiple KDC

We had a requirement to stop and restart the KDC on multiple remote DCs.

Rather than go around each one , we wrote a script .

function DateAndTimeLog
{
Get-date
}
function testIf
{
If (Test-Connection -comp $dc -count 1 -quiet) {
$dc+" is online! "
}
Else {
$dc+" is offline, moving to next Domain Controller"
continue;
}

}
function SlowItDown
{
Start-Sleep -s 5
}

function MessageStop
{
Write-Host "stopping the KDC on $DC "
Write-Output "stopping the KDC on $DC "
}

function MessageStart
{
Write-Host "now restarting the KDC on $DC "
Write-Output "now restarting the KDC on $DC "
Write-Output "finished process"
Write-Output " ------------------ "
}

$File ="C:DCResults.txt"
$DCsinOurDomain= Get-ADDomainController -Filter *

ForEach($DC in $DCsinOurDomain.HostName){
DateAndTimeLog | Out-File $File -Append
testif | ft | Out-File $File -Append
(Get-WmiObject Win32_Service -filter "name='kdc'" -ComputerName $DC).StopService()
MessageStop | Out-File $File -Append
SlowItDown
(Get-WmiObject Win32_Service -filter "name='kdc'" -ComputerName $DC).StartService()
MessageStart | Out-File $File -Append

}