perling/setup-task-scheduler.ps1

53 lines
2.3 KiB
PowerShell

# PowerShell Script to Create Windows Task Scheduler for Perizinan Data Sync
# Run this script as Administrator
$TaskName = "PerizinanDataSync"
$TaskDescription = "Daily sync of perizinan data from Jakarta API at midnight"
$ScriptPath = "C:\laragon\www\perling\sync-perizinan.bat"
$LogPath = "C:\laragon\www\perling\storage\logs\task-scheduler.log"
Write-Host "Creating Windows Task Scheduler for Perizinan Data Sync..." -ForegroundColor Green
# Check if task already exists
$ExistingTask = Get-ScheduledTask -TaskName $TaskName -ErrorAction SilentlyContinue
if ($ExistingTask) {
Write-Host "Task '$TaskName' already exists. Removing old task..." -ForegroundColor Yellow
Unregister-ScheduledTask -TaskName $TaskName -Confirm:$false
}
try {
# Create Task Action
$Action = New-ScheduledTaskAction -Execute $ScriptPath
# Create Task Trigger (Daily at midnight)
$Trigger = New-ScheduledTaskTrigger -Daily -At "00:00"
# Create Task Settings
$Settings = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries -StartWhenAvailable
# Create Task Principal (Run with highest privileges)
$Principal = New-ScheduledTaskPrincipal -UserId "SYSTEM" -LogonType ServiceAccount -RunLevel Highest
# Register the Task
Register-ScheduledTask -TaskName $TaskName -Action $Action -Trigger $Trigger -Settings $Settings -Principal $Principal -Description $TaskDescription
Write-Host "✅ Task Scheduler created successfully!" -ForegroundColor Green
Write-Host "Task Name: $TaskName" -ForegroundColor Cyan
Write-Host "Schedule: Daily at 00:00 (midnight)" -ForegroundColor Cyan
Write-Host "Script Path: $ScriptPath" -ForegroundColor Cyan
# Show task info
Get-ScheduledTask -TaskName $TaskName | Format-Table -AutoSize
} catch {
Write-Host "❌ Error creating task scheduler: $($_.Exception.Message)" -ForegroundColor Red
Write-Host "Please run this script as Administrator" -ForegroundColor Yellow
}
Write-Host "`nTo manually run the task, use:" -ForegroundColor Cyan
Write-Host "Start-ScheduledTask -TaskName '$TaskName'" -ForegroundColor White
Write-Host "`nTo view task history:" -ForegroundColor Cyan
Write-Host "Get-WinEvent -FilterHashtable @{LogName='Microsoft-Windows-TaskScheduler/Operational'; ID=102}" -ForegroundColor White