Files
windows-builder/includes/$OEM$/$$/OEM/scripts/windowsupdate/Setup-ScheduledTask.ps1
2026-06-02 03:37:09 -07:00

159 lines
5.1 KiB
PowerShell

<#
.SYNOPSIS
Create Scheduled Task for Windows Update Manager
.DESCRIPTION
Sets up automated Windows update checks with notifications
.NOTES
Author: oxmc
#>
#Requires -RunAsAdministrator
Write-Host "=== Windows Update Manager - Scheduled Task Setup ===" -ForegroundColor Cyan
Write-Host ""
$scriptPath = "$PSScriptRoot\WindowsUpdateManager.ps1"
if (-not (Test-Path $scriptPath)) {
Write-Host "ERROR: WindowsUpdateManager.ps1 not found in $PSScriptRoot" -ForegroundColor Red
pause
exit 1
}
Write-Host "Script location: $scriptPath" -ForegroundColor Gray
Write-Host ""
# Task configuration options
Write-Host "Configure scheduled update checks:" -ForegroundColor Yellow
Write-Host ""
Write-Host "1. Check every 12 hours (recommended)" -ForegroundColor White
Write-Host "2. Check daily at 3:00 AM" -ForegroundColor White
Write-Host "3. Check weekly on Sunday at 3:00 AM" -ForegroundColor White
Write-Host "4. Custom schedule" -ForegroundColor White
Write-Host ""
Write-Host "Select option (1-4): " -NoNewline -ForegroundColor Yellow
$choice = Read-Host
$trigger = $null
$taskName = "WindowsUpdateManager-AutoCheck"
switch ($choice) {
"1" {
# Every 12 hours
Write-Host "Creating task to run every 12 hours..." -ForegroundColor Green
$trigger1 = New-ScheduledTaskTrigger -Daily -At "6:00AM"
$trigger2 = New-ScheduledTaskTrigger -Daily -At "6:00PM"
$trigger = @($trigger1, $trigger2)
}
"2" {
# Daily at 3 AM
Write-Host "Creating daily task at 3:00 AM..." -ForegroundColor Green
$trigger = New-ScheduledTaskTrigger -Daily -At "3:00AM"
}
"3" {
# Weekly
Write-Host "Creating weekly task (Sunday 3:00 AM)..." -ForegroundColor Green
$trigger = New-ScheduledTaskTrigger -Weekly -DaysOfWeek Sunday -At "3:00AM"
}
"4" {
# Custom
Write-Host "Enter time (HH:MM format, e.g., 14:30): " -NoNewline -ForegroundColor Yellow
$time = Read-Host
Write-Host "Creating daily task at $time..." -ForegroundColor Green
$trigger = New-ScheduledTaskTrigger -Daily -At $time
}
default {
Write-Host "Invalid choice. Using default (every 12 hours)..." -ForegroundColor Yellow
$trigger1 = New-ScheduledTaskTrigger -Daily -At "6:00AM"
$trigger2 = New-ScheduledTaskTrigger -Daily -At "6:00PM"
$trigger = @($trigger1, $trigger2)
}
}
# Auto-install option
Write-Host ""
Write-Host "Should updates be installed automatically? (Y/N): " -NoNewline -ForegroundColor Yellow
$autoInstall = Read-Host
$arguments = "-NoProfile -ExecutionPolicy Bypass -WindowStyle Hidden -File `"$scriptPath`""
if ($autoInstall -eq 'Y' -or $autoInstall -eq 'y') {
$arguments += " -AutoInstall"
Write-Host "Auto-install enabled" -ForegroundColor Green
}
else {
Write-Host "Auto-install disabled - notifications only" -ForegroundColor Yellow
}
# Create the action
$action = New-ScheduledTaskAction -Execute "PowerShell.exe" -Argument $arguments
# Create principal (run as SYSTEM)
$principal = New-ScheduledTaskPrincipal -UserId "SYSTEM" -RunLevel Highest -LogonType ServiceAccount
# Create settings
$settings = New-ScheduledTaskSettingsSet `
-AllowStartIfOnBatteries `
-DontStopIfGoingOnBatteries `
-StartWhenAvailable `
-RunOnlyIfNetworkAvailable `
-MultipleInstances IgnoreNew
# Remove existing task if present
try {
Unregister-ScheduledTask -TaskName $taskName -Confirm:$false -ErrorAction SilentlyContinue
Write-Host "Removed existing task" -ForegroundColor Gray
}
catch {
# Task didn't exist, that's fine
}
# Register the task
try {
Register-ScheduledTask -TaskName $taskName `
-Action $action `
-Trigger $trigger `
-Principal $principal `
-Settings $settings `
-Description "Automatically checks for Windows updates and shows notifications" | Out-Null
Write-Host ""
Write-Host "✓ Scheduled task created successfully!" -ForegroundColor Green
Write-Host ""
Write-Host "Task Name: $taskName" -ForegroundColor Gray
Write-Host "Task will run as: SYSTEM (highest privileges)" -ForegroundColor Gray
Write-Host ""
Write-Host "To view or modify the task:" -ForegroundColor Cyan
Write-Host " 1. Open Task Scheduler (taskschd.msc)" -ForegroundColor White
Write-Host " 2. Navigate to Task Scheduler Library" -ForegroundColor White
Write-Host " 3. Find '$taskName'" -ForegroundColor White
Write-Host ""
# Test run option
Write-Host "Would you like to test run the task now? (Y/N): " -NoNewline -ForegroundColor Yellow
$testRun = Read-Host
if ($testRun -eq 'Y' -or $testRun -eq 'y') {
Write-Host "Running task..." -ForegroundColor Green
Start-ScheduledTask -TaskName $taskName
Write-Host "Task started. Check for notifications in a few moments." -ForegroundColor Green
}
}
catch {
Write-Host ""
Write-Host "✗ Failed to create scheduled task" -ForegroundColor Red
Write-Host "Error: $($_.Exception.Message)" -ForegroundColor Red
pause
exit 1
}
Write-Host ""
Write-Host "=== Setup Complete ===" -ForegroundColor Green
pause