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

Write-Host "Creating Sync Task via XML..." -ForegroundColor Cyan

Invoke-Command -ComputerName 192.168.0.6 -Credential $cred -ScriptBlock {
    # Create task XML
    $taskXml = @'
<?xml version="1.0" encoding="UTF-16"?>
<Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
  <RegistrationInfo>
    <Description>Sync test data and software updates between NAS and AD2</Description>
  </RegistrationInfo>
  <Triggers>
    <TimeTrigger>
      <Repetition>
        <Interval>PT15M</Interval>
        <StopAtDurationEnd>false</StopAtDurationEnd>
      </Repetition>
      <StartBoundary>2026-01-20T10:00:00</StartBoundary>
      <Enabled>true</Enabled>
    </TimeTrigger>
  </Triggers>
  <Principals>
    <Principal id="Author">
      <UserId>INTRANET\sysadmin</UserId>
      <LogonType>Password</LogonType>
      <RunLevel>HighestAvailable</RunLevel>
    </Principal>
  </Principals>
  <Settings>
    <MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>
    <DisallowStartIfOnBatteries>false</DisallowStartIfOnBatteries>
    <StopIfGoingOnBatteries>false</StopIfGoingOnBatteries>
    <AllowHardTerminate>true</AllowHardTerminate>
    <StartWhenAvailable>true</StartWhenAvailable>
    <RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>
    <AllowStartOnDemand>true</AllowStartOnDemand>
    <Enabled>true</Enabled>
    <ExecutionTimeLimit>PT30M</ExecutionTimeLimit>
  </Settings>
  <Actions Context="Author">
    <Exec>
      <Command>powershell.exe</Command>
      <Arguments>-ExecutionPolicy Bypass -NonInteractive -File "C:\Shares\test\scripts\Sync-FromNAS.ps1"</Arguments>
      <WorkingDirectory>C:\Shares\test\scripts</WorkingDirectory>
    </Exec>
  </Actions>
</Task>
'@

    # Save XML to file
    $xmlPath = "C:\Temp\sync-task.xml"
    $taskXml | Out-File -FilePath $xmlPath -Encoding Unicode -Force

    Write-Host "[1] Deleting old task if exists..." -ForegroundColor Yellow
    schtasks /Delete /TN "Sync-FromNAS" /F 2>$null

    Write-Host "[2] Creating task from XML..." -ForegroundColor Yellow
    $result = schtasks /Create /XML $xmlPath /TN "Sync-FromNAS" /RU "INTRANET\sysadmin" /RP "Paper123!@#" /F
    Write-Host "  $result" -ForegroundColor White

    Write-Host ""
    Write-Host "[3] Starting task..." -ForegroundColor Yellow
    schtasks /Run /TN "Sync-FromNAS"

    Write-Host ""
    Write-Host "  Waiting 30 seconds for sync..." -ForegroundColor White
    Start-Sleep -Seconds 30

    Write-Host ""
    Write-Host "[4] Checking task status..." -ForegroundColor Yellow
    schtasks /Query /TN "Sync-FromNAS" /FO LIST /V

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