PowerShell Code

Remove-DNSRecords.ps1

View-only PowerShell code for review.

<#
.SYNOPSIS
    Confirms all domain controllers are syncing time properly.
.DESCRIPTION
    This script retrieves all domain controllers in the current Active Directory domain and checks their time synchronization status using the w32tm /query /status command. The results are displayed in a formatted table, showing each domain controller's name and its time synchronization information.
.EXAMPLE
    .\Get-Time.ps1
    This command runs the script, which retrieves the time synchronization status for all domain controllers in the current Active Directory domain and displays the results in a formatted table.  
.EXAMPLE
    Get-Time.ps1 | Out-File -Append -FilePath "TimeSyncReport.txt"
    This command runs the script and appends the output to a text file named "TimeSyncReport.txt" in the current directory.
.INPUTS
    None. This script does not accept any input from the pipeline.
.OUTPUTS
    The script outputs a formatted table containing the name of each domain controller and its time synchronization information, including the time source, last sync time, and any errors related to time synchronization.
.NOTES
    Assure that you have the necessary permissions to access the domain controllers and run the w32tm command remotely.
    The script uses PowerShell remoting to connect to each domain controller, so ensure that PowerShell remoting is enabled and properly configured in your environment.

# Confirms all domain controllers are syncing time properly

# Get all domain controllers in the current domain
$DomainControllers = Get-ADDomainController -Filter *

# Check time source and last sync for each DC
$Results = foreach ($DC in $DomainControllers) {
    $Session = New-PSSession -ComputerName $DC.HostName -ErrorAction SilentlyContinue
    if ($Session) {
        $W32Time = Invoke-Command -Session $Session -ScriptBlock {
            w32tm /query /status
        }
        Remove-PSSession $Session
        [PSCustomObject]@{
            DCName    = $DC.HostName
            TimeInfo  = $W32Time -join "`n"
        }
    } else {
        [PSCustomObject]@{
            DCName    = $DC.HostName
            TimeInfo  = "Unable to connect"
        }
    }
}

# Output results
$Results | Format-Table -AutoSize