Script PowerShell NTP Server compatibile con Windows Server 2012 R2
# Funzione per controllare lo stato del servizio w32time
function Check-ServiceStatus {
$service = Get-Service -Name w32time -ErrorAction SilentlyContinue
if ($service -eq $null) {
Write-Host "`n[ERRORE] Il servizio Windows Time (w32time) non è installato." -ForegroundColor Red
exit 1
}
Write-Host "`n[INFO] Stato attuale del servizio Windows Time: $($service.Status)" -ForegroundColor Cyan
if ($service.Status -ne 'Running') {
$response = Read-Host "[DOMANDA] Il servizio è arrestato. Vuoi avviarlo? (s/n)"
if ($response -eq 's') {
Set-Service -Name w32time -StartupType Automatic
Start-Service w32time
Start-Sleep -Seconds 3
Write-Host "[OK] Il servizio Windows Time è stato avviato!" -ForegroundColor Green
} else {
Write-Host "[ERRORE] Il servizio non è stato avviato. Interruzione dello script." -ForegroundColor Red
exit 1
}
} else {
Write-Host "[OK] Il servizio Windows Time è già in esecuzione." -ForegroundColor Green
}
}
# Funzione per configurare i server NTP
function Configure-NTP {
Write-Host "`n[INFO] Server NTP attuali:"
w32tm /query /peers
$response = Read-Host "`n[DOMANDA] Vuoi modificare l'elenco dei server NTP? (s/n)"
if ($response -eq 's') {
$ntpList = Read-Host "Inserisci i server NTP separati da spazio (es: it.pool.ntp.org,0x8 ntp1.inrim.it,0x8)"
} else {
$ntpList = "it.pool.ntp.org,0x8 tempo.cstv.to.cnr.it,0x8 ntp1.inrim.it,0x8 ntp2.inrim.it,0x8"
}
Write-Host "[INFO] Configurazione dei server NTP..."
w32tm /config /manualpeerlist:"$ntpList" /syncfromflags:manual /reliable:YES /update
Write-Host "[OK] Configurazione completata." -ForegroundColor Green
}
# Funzione per forzare la sincronizzazione
function Sync-NTP {
$response = Read-Host "`n[DOMANDA] Vuoi forzare immediatamente la sincronizzazione dell'ora? (s/n)"
if ($response -eq 's') {
Write-Host "[INFO] Forzatura della sincronizzazione NTP..."
w32tm /resync
Start-Sleep -Seconds 3
Write-Host "[OK] Sincronizzazione forzata." -ForegroundColor Green
} else {
Write-Host "[INFO] Sincronizzazione saltata su richiesta dell'utente." -ForegroundColor Yellow
}
}
# Funzione per verificare lo stato della sincronizzazione
function Check-NTPStatus {
Write-Host "`n[INFO] Stato della sincronizzazione NTP:"
w32tm /query /status
}
# Esecuzione dello script
Write-Host "[INFO] Avvio della configurazione NTP su Windows Server..." -ForegroundColor Cyan
Check-ServiceStatus
Configure-NTP
Sync-NTP
Check-NTPStatus
Write-Host "`n[OK] Configurazione completata con successo!" -ForegroundColor Cyan