function Show-Menu {
    Clear-Host
    Write-Host "=============================="
    Write-Host " Time Sync Configuration Menu "
    Write-Host "=============================="
    Write-Host "1. Check NTP Server Configuration"
    Write-Host "2. Sync Time with Domain Controller"
    Write-Host "3. Set to Nearest NTP Server"
    Write-Host "4. Exit"
}

function Check-NTPConfiguration {
    $ntpServer = Get-WmiObject -Class Win32_ComputerSystem | Select-Object -ExpandProperty DomainRole
    if ($ntpServer -eq 0) {
        Write-Host "This computer is not part of a domain."
    } else {
        $currentNTP = w32tm /query /status | Select-String "Source"
        Write-Host "Current NTP Server: $currentNTP"
    }
    Pause
}

function Sync-TimeWithDC {
    $dc = Read-Host "Enter Domain Controller FQDN or IP Address"
    w32tm /config /manualpeerlist:$dc /syncfromflags:manual /reliable:YES /update
    w32tm /resync
    Write-Host "Time synchronized with $dc."
    Pause
}

function Set-ToNearestNTP {
    w32tm /config /manualpeerlist:"time.windows.com,0x1" /syncfromflags:manual /reliable:YES /update
    w32tm /resync
    Write-Host "Time synchronized with nearest NTP server (time.windows.com)."
    Pause
}

do {
    Show-Menu
    $choice = Read-Host "Please enter your choice (1-4)"
    
    switch ($choice) {
        1 { Check-NTPConfiguration }
        2 { Sync-TimeWithDC }
        3 { Set-ToNearestNTP }
        4 { Write-Host "Exiting..."; break }
        default { Write-Host "Invalid choice. Please select again." }
    }
} while ($true)
