84 lines
3.6 KiB
PowerShell
84 lines
3.6 KiB
PowerShell
$password = ConvertTo-SecureString 'Paper123!@#' -AsPlainText -Force
|
|
$cred = New-Object System.Management.Automation.PSCredential('INTRANET\sysadmin', $password)
|
|
|
|
Write-Host "Recreating Sync-FromNAS Task..." -ForegroundColor Cyan
|
|
Write-Host ""
|
|
|
|
Invoke-Command -ComputerName 192.168.0.6 -Credential $cred -ScriptBlock {
|
|
# Task parameters
|
|
$taskName = "Sync-FromNAS"
|
|
$scriptPath = "C:\Shares\test\scripts\Sync-FromNAS.ps1"
|
|
$logPath = "C:\Shares\test\scripts\sync-from-nas.log"
|
|
$taskUser = "INTRANET\sysadmin"
|
|
$taskPassword = 'Paper123!@#'
|
|
|
|
# Remove existing task if it exists
|
|
Unregister-ScheduledTask -TaskName $taskName -Confirm:$false -ErrorAction SilentlyContinue
|
|
|
|
Write-Host "[1] Creating task action..." -ForegroundColor Yellow
|
|
$action = New-ScheduledTaskAction `
|
|
-Execute "powershell.exe" `
|
|
-Argument "-ExecutionPolicy Bypass -NonInteractive -File `"$scriptPath`"" `
|
|
-WorkingDirectory "C:\Shares\test\scripts"
|
|
|
|
Write-Host "[2] Creating task trigger (every 15 minutes)..." -ForegroundColor Yellow
|
|
$trigger = New-ScheduledTaskTrigger `
|
|
-Once `
|
|
-At (Get-Date).AddMinutes(1) `
|
|
-RepetitionInterval (New-TimeSpan -Minutes 15) `
|
|
-RepetitionDuration ([TimeSpan]::MaxValue)
|
|
|
|
Write-Host "[3] Creating task settings..." -ForegroundColor Yellow
|
|
$settings = New-ScheduledTaskSettingsSet `
|
|
-AllowStartIfOnBatteries `
|
|
-DontStopIfGoingOnBatteries `
|
|
-StartWhenAvailable `
|
|
-ExecutionTimeLimit (New-TimeSpan -Minutes 30) `
|
|
-RestartCount 3 `
|
|
-RestartInterval (New-TimeSpan -Minutes 1)
|
|
|
|
Write-Host "[4] Registering task as $taskUser..." -ForegroundColor Yellow
|
|
Register-ScheduledTask `
|
|
-TaskName $taskName `
|
|
-Action $action `
|
|
-Trigger $trigger `
|
|
-Settings $settings `
|
|
-User $taskUser `
|
|
-Password $taskPassword `
|
|
-RunLevel Highest `
|
|
-Description "Sync test data and software updates between NAS and AD2" `
|
|
-Force | Out-Null
|
|
|
|
Write-Host " [OK] Task created successfully" -ForegroundColor Green
|
|
|
|
Write-Host ""
|
|
Write-Host "[5] Verifying task..." -ForegroundColor Yellow
|
|
$task = Get-ScheduledTask -TaskName $taskName
|
|
Write-Host " User: $($task.Principal.UserId)" -ForegroundColor White
|
|
Write-Host " State: $($task.State)" -ForegroundColor White
|
|
|
|
Write-Host ""
|
|
Write-Host "[6] Starting task now..." -ForegroundColor Yellow
|
|
Start-ScheduledTask -TaskName $taskName
|
|
Write-Host " Waiting 25 seconds for sync to complete..." -ForegroundColor White
|
|
Start-Sleep -Seconds 25
|
|
|
|
Write-Host ""
|
|
Write-Host "[7] Checking results..." -ForegroundColor Yellow
|
|
$taskInfo = Get-ScheduledTaskInfo -TaskName $taskName
|
|
Write-Host " Last Run: $($taskInfo.LastRunTime)" -ForegroundColor White
|
|
Write-Host " Last Result: 0x$($taskInfo.LastTaskResult.ToString('X')) $(if ($taskInfo.LastTaskResult -eq 0) { '(SUCCESS)' } else { '(FAILED)' })" -ForegroundColor $(if ($taskInfo.LastTaskResult -eq 0) { "Green" } else { "Red" })
|
|
Write-Host " Next Run: $($taskInfo.NextRunTime)" -ForegroundColor White
|
|
|
|
Write-Host ""
|
|
Write-Host "[8] Last 20 lines of sync log..." -ForegroundColor Yellow
|
|
if (Test-Path $logPath) {
|
|
Get-Content $logPath | Select-Object -Last 20 | ForEach-Object { Write-Host " $_" -ForegroundColor Gray }
|
|
}
|
|
}
|
|
|
|
Write-Host ""
|
|
Write-Host "================================================" -ForegroundColor Cyan
|
|
Write-Host "Task Recreation Complete" -ForegroundColor Cyan
|
|
Write-Host "================================================" -ForegroundColor Cyan
|