$password = ConvertTo-SecureString 'Paper123!@#' -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential('INTRANET\sysadmin', $password)

Write-Host "================================================" -ForegroundColor Cyan
Write-Host "Fixing Sync Task User Account" -ForegroundColor Cyan
Write-Host "================================================" -ForegroundColor Cyan
Write-Host ""

Invoke-Command -ComputerName 192.168.0.6 -Credential $cred -ScriptBlock {
    Write-Host "[1] Checking current task configuration..." -ForegroundColor Yellow
    $task = Get-ScheduledTask -TaskName "Sync-FromNAS"
    Write-Host "  Current user: $($task.Principal.UserId)" -ForegroundColor White
    Write-Host "  Run level: $($task.Principal.RunLevel)" -ForegroundColor White

    Write-Host ""
    Write-Host "[2] Reconfiguring task to run as sysadmin..." -ForegroundColor Yellow

    # Get the current task definition
    $taskAction = $task.Actions[0]
    $taskTrigger = $task.Triggers[0]
    $taskSettings = $task.Settings

    # Create password for sysadmin
    $taskPassword = 'Paper123!@#'

    # Unregister old task
    Unregister-ScheduledTask -TaskName "Sync-FromNAS" -Confirm:$false

    # Register new task with sysadmin user
    $action = New-ScheduledTaskAction -Execute $taskAction.Execute -Argument $taskAction.Arguments -WorkingDirectory $taskAction.WorkingDirectory
    $trigger = New-ScheduledTaskTrigger -Once -At (Get-Date) -RepetitionInterval (New-TimeSpan -Minutes 15) -RepetitionDuration ([TimeSpan]::MaxValue)
    $settings = New-ScheduledTaskSettingsSet `
        -AllowStartIfOnBatteries `
        -DontStopIfGoingOnBatteries `
        -StartWhenAvailable `
        -ExecutionTimeLimit (New-TimeSpan -Minutes 30)

    Register-ScheduledTask `
        -TaskName "Sync-FromNAS" `
        -Action $action `
        -Trigger $trigger `
        -Settings $settings `
        -User "INTRANET\sysadmin" `
        -Password $taskPassword `
        -RunLevel Highest `
        -Force | Out-Null

    Write-Host "  [OK] Task reconfigured to run as INTRANET\sysadmin" -ForegroundColor Green

    Write-Host ""
    Write-Host "[3] Verifying new configuration..." -ForegroundColor Yellow
    $newTask = Get-ScheduledTask -TaskName "Sync-FromNAS"
    Write-Host "  New user: $($newTask.Principal.UserId)" -ForegroundColor White
    Write-Host "  State: $($newTask.State)" -ForegroundColor White

    Write-Host ""
    Write-Host "[4] Testing sync task..." -ForegroundColor Yellow
    Start-ScheduledTask -TaskName "Sync-FromNAS"
    Write-Host "  Task started - waiting 20 seconds..." -ForegroundColor White
    Start-Sleep -Seconds 20

    Write-Host ""
    Write-Host "[5] Checking sync log..." -ForegroundColor Yellow
    if (Test-Path "C:\Shares\test\scripts\sync-from-nas.log") {
        $lastLog = Get-Content "C:\Shares\test\scripts\sync-from-nas.log" | Select-Object -Last 15
        $lastLog | ForEach-Object { Write-Host "  $_" -ForegroundColor Gray }
    }

    Write-Host ""
    Write-Host "[6] Checking task status..." -ForegroundColor Yellow
    $taskInfo = Get-ScheduledTaskInfo -TaskName "Sync-FromNAS"
    Write-Host "  Last Run: $($taskInfo.LastRunTime)" -ForegroundColor White
    Write-Host "  Last Result: 0x$($taskInfo.LastTaskResult.ToString('X'))" -ForegroundColor $(if ($taskInfo.LastTaskResult -eq 0) { "Green" } else { "Red" })
    Write-Host "  Next Run: $($taskInfo.NextRunTime)" -ForegroundColor White
}

Write-Host ""
Write-Host "================================================" -ForegroundColor Cyan
Write-Host "Task User Fix Complete" -ForegroundColor Cyan
Write-Host "================================================" -ForegroundColor Cyan
