หลังจากเขียน Winget: update script แล้วอยากให้มัน update / upgrade ด้วยตัวเอง แบบ Automate ขึ้นมา เลย คิดมาได้ว่าใน Windows มันมี Task Scheduler นี่น่า ทำไม่ไม่ใส่ไว้ใน scheduler ให้ถึงเวลาพักเที่ยงแล้วให้มันทำงานเอง
add_task_scheduler.ps1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | # Get the full path to the script. Handles relative and absolute paths. $scriptPath = Resolve-Path .\winget_update_major_minor_change.ps1 # Check if the script exists. if (!( Test-Path $scriptPath )) { Write-Error "Error: Script '$scriptPath' not found. Check the path and ensure the script exists." exit 1 } # Task name $taskName = "WingetUpdateWeekly" # Check if the task already exists and remove it if necessary. Improved error handling try { $existingTask = Get-ScheduledTask -TaskName $taskName -ErrorAction SilentlyContinue # SilentlyContinue for the case of not finding the task. if ( $existingTask ) { Write-Warning "Scheduled task '$taskName' already exists. Removing existing task..." Remove-ScheduledTask -TaskName $taskName -Force -ErrorAction Stop # Stop on error during removal } } catch { #This catch block now handles the error of *not finding* the task properly. Write-Warning "No existing task found. Proceeding to create a new one." } # Create the scheduled task $trigger = New-ScheduledTaskTrigger -Weekly -DaysOfWeek Wednesday -At 12:00:00 $powershellPath = ( Get-Command powershell.exe).Source $action = New-ScheduledTaskAction -Execute $powershellPath -Argument "-ExecutionPolicy Bypass -File `"$scriptPath`"" try { Register-ScheduledTask -Action $action -Force -RunLevel Highest -TaskName $taskName -Trigger $trigger Write-Host " Scheduled task '$taskName' created successfully. It will run every Wednesday at noon with administrator privileges. " } catch { Write-Error " Error creating scheduled task: $_ " exit 1 } |
เรียกใช้โดยคำสั่ง ./add_task_scheduler.ps1
ถ้ามีข้อความ
TaskPath TaskName
——– ——–
\ WingetUpd…
Scheduled task ‘WingetUpdateWeekly’ created successfully. It will run every Wednesday at noon with administrator privileges.
อันนี้คือตั้งเวลาสำเร็จแล้ว ปรับแต่งได้อีกครั้งในโปรแกรม Task Scheduler เช่น เปลี่ยนเวลา set ให้ run โดย user อื่น ๆ
อ่านเพิ่มเติม