Created
October 13, 2025 03:31
-
-
Save valtoni/47f6307e7529df474c3b38cd687898af to your computer and use it in GitHub Desktop.
Windows Choco Update Daily Schedule
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Garante que a fonte existe | |
| if (-not [System.Diagnostics.EventLog]::SourceExists("ChocoUpgrade")) { | |
| New-EventLog -LogName Application -Source "ChocoUpgrade" | |
| } | |
| function Write-EventLogChoco($EventId, $Message) { | |
| Write-EventLog -LogName Application -Source "ChocoUpgrade" -EntryType Information -EventId $EventId -Message $Message | |
| } | |
| $updatedPackages = 0 | |
| # Pega os pacotes desatualizados de forma limpa | |
| $outdated = choco outdated --no-color --limit-output | ForEach-Object { | |
| $parts = $_ -split '\|' | |
| [PSCustomObject]@{ | |
| Name = $parts[0] | |
| CurrentVer = $parts[1] | |
| AvailableVer = $parts[2] | |
| } | |
| } | |
| foreach ($pkg in $outdated) { | |
| try { | |
| $result = choco upgrade $pkg.Name -y --no-color | Out-String | |
| Write-EventLogChoco 1001 "Atualizado: $($pkg.Name) de $($pkg.CurrentVer) para $($pkg.AvailableVer)`n$result" | |
| $updatedPackages++ | |
| } catch { | |
| Write-EventLogChoco 1002 "Erro ao atualizar $($pkg.Name): $($_.Exception.Message)" | |
| } | |
| } | |
| Write-EventLogChoco 1001 "Fim da da checagem`n${updatedPackages} pacotes foram atualizados" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $action = New-ScheduledTaskAction -Execute "pwsh" -Argument '-ExecutionPolicy Bypass -File "C:\work\workspace\homelab\chocoupdate-schedule\choco-upgrade-log.ps1"' | |
| $trigger = New-ScheduledTaskTrigger -Daily -At 12:00AM | |
| $principal = New-ScheduledTaskPrincipal -UserId "SYSTEM" -LogonType ServiceAccount -RunLevel Highest | |
| Register-ScheduledTask -TaskName "ChocoUpgradeDayly" -Action $action -Trigger $trigger -Principal $principal |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment