<#Created By Abhishek Bansal
Read Note
Script Usage :: Useful in fetching sync time source for all the domain controllers of the domain.
Pre requisites :: Copy all the code into a text file, save it with an extension ".PS1". Once saved, run it as Administrator.
Execution & Outputs :: Once executed, there will be two files created as Timesyncdetails.csv containing output and Errorlogs.csv for any errors. #>
$dcs = (Get-ADDomainController -Filter *).Name
Write-Host -ForegroundColor Green "$((Get-ADDomain).PDCEmulator) is holding PDC role.."
$line = 0
$linecount = $dcs.Count
$pct = 0
foreach($dc in $dcs)
{
$line++
$dc = $dc.Trim()
$pct = $line/$linecount * 100
try{
$error.Clear()
Write-Progress -Activity " " -PercentComplete $pct
Invoke-Command -ComputerName $dc -ScriptBlock{"`nFetching Sync time for $($using:dc)"} -ErrorAction Stop
$dc | Select-Object -Property @{n="Hostname";e={$dc}},@{n="Source";e={Invoke-Command -ComputerName $dc -ScriptBlock{w32tm /query /source}}} | Export-Csv ./Timesyncdetails.csv -NoTypeInformation -Append
}
catch{
$dc | Select-Object -Property @{n="Hostname";e={$dc}},@{n="ErrorInfo";e={$($error.exception.Message)}} | Export-Csv ./Errorlogs.csv -NoTypeInformation -Append
}
}