Service Auto Restart Script

# =========================
# AYARLAR
# =========================
$serviceName = "BURAYA_GERCEK_SERVICE_NAME"
$startTime = Get-Date "2026-03-27 11:30:00"
$intervalMinutes = 5

# =========================
# SCRIPT
# =========================
$scriptPath = "C:\Scripts\service-check.ps1"

New-Item -ItemType Directory -Path "C:\Scripts" -Force | Out-Null

@"
`$ServiceName = "$serviceName"
`$logPath = "C:\Scripts\service-check.log"

function Write-Log {
    param([string]`$msg)
    `$date = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
    Add-Content -Path `$logPath -Value "`$date - `$msg"
}

try {
    `$service = Get-Service -Name `$ServiceName -ErrorAction Stop

    if (`$service.Status -eq 'Stopped') {
        Write-Log "Servis stopped, start deneniyor"
        Start-Service -Name `$ServiceName -ErrorAction Stop
        Start-Sleep -Seconds 15

        `$service = Get-Service -Name `$ServiceName
        if (`$service.Status -eq 'Running') {
            Write-Log "Servis basariyla calisiyor"
        }
        else {
            Write-Log "Servis start sonrasi running olmadi. Durum: `$(`$service.Status)"
        }
    }
    elseif (`$service.Status -eq 'Running') {
        Write-Log "Servis zaten calisiyor"
    }
    else {
        Write-Log "Servis ara durumda: `$(`$service.Status)"
    }
}
catch {
    Write-Log "Hata: `$(`$_.Exception.Message)"
}
"@ | Set-Content -Path $scriptPath -Encoding UTF8

$action = New-ScheduledTaskAction -Execute "powershell.exe" -Argument "-ExecutionPolicy Bypass -File `"$scriptPath`""

$trigger = New-ScheduledTaskTrigger `
    -Once -At $startTime `
    -RepetitionInterval (New-TimeSpan -Minutes $intervalMinutes) `
    -RepetitionDuration (New-TimeSpan -Days 3650)

$principal = New-ScheduledTaskPrincipal -UserId "SYSTEM" -RunLevel Highest

Register-ScheduledTask -TaskName "ServiceAutoRestart_fc8" -Action $action -Trigger $trigger -Principal $principal -Force


============================================000
$ServiceName = 'fc8 C:\Fidelio\IFC8\MIK\MIK.XML'
$logPath = 'C:\Scripts\service-check.log'

function Write-Log {
    param([string]$Text)
    $ts = Get-Date -Format 'yyyy-MM-dd HH:mm:ss'
    Add-Content -Path $logPath -Value "$ts - $Text"
}

try {
    $svc = Get-Service -Name $ServiceName -ErrorAction Stop

    Write-Log "Restart basladi: $ServiceName"

    if ($svc.Status -eq 'Running') {
        Stop-Service -Name $ServiceName -Force -ErrorAction SilentlyContinue
        Start-Sleep -Seconds 5
    }

    Start-Service -Name $ServiceName -ErrorAction Stop
    Start-Sleep -Seconds 10

    $svc = Get-Service -Name $ServiceName -ErrorAction Stop
    Write-Log "Son durum: $($svc.Status)"
}
catch {
    Write-Log "HATA: $($_.Exception.Message)"
}

==============================================================

$taskName = "ServiceAutoRestart_fc8_MIK"
$scriptPath = "C:\Scripts\service-check.ps1"
$intervalMinutes = 5

schtasks /Delete /TN $taskName /F 2>$null

schtasks /Create `
 /SC MINUTE `
 /MO $intervalMinutes `
 /TN $taskName `
 /TR "powershell.exe -ExecutionPolicy Bypass -File `"$scriptPath`"" `
 /RU SYSTEM `
 /RL HIGHEST `
 /F
 
 ======================
 $taskName = "ServiceAutoRestart_fc8_MIK"
$scriptPath = "C:\Scripts\service-check.ps1"

schtasks /Delete /TN $taskName /F 2>$null

schtasks /Create `
 /SC DAILY `
 /ST 03:00 `
 /TN $taskName `
 /TR "powershell.exe -ExecutionPolicy Bypass -File `"$scriptPath`"" `
 /RU SYSTEM `
 /RL HIGHEST `
 /F