Set up automated maintenance tasks to keep Chocolatey updated and ensure optimal performance across your device fleet.
Create a scheduled task script for automatic Chocolatey updates:
# Chocolatey Auto-Update Script
# Save as: ChocolateyMaintenance.ps1
Start-Transcript -Path "$env:ProgramData\chocolatey\logs\maintenance.log" -Append
try {
Write-Output "Starting Chocolatey maintenance - $(Get-Date)"
# Update Chocolatey itself
Write-Output "Updating Chocolatey..."
& choco upgrade chocolatey -y
# Update all installed packages
Write-Output "Updating all packages..."
& choco upgrade all -y
# Clean up temporary files
Write-Output "Cleaning temporary files..."
& choco cache clean
Write-Output "Maintenance completed successfully - $(Get-Date)"
} catch {
Write-Error "Maintenance failed: $($_.Exception.Message)"
} finally {
Stop-Transcript
}
Deploy this maintenance script as another Win32 app with these settings:
- Install command:
powershell.exe -executionpolicy bypass -file ChocolateyMaintenance.ps1 - Detection rule: Script-based detection checking last run timestamp
- Assignment: Required, recurring every 7 days
Create a detection script for the maintenance task:
# Maintenance Detection Script
$logPath = "$env:ProgramData\chocolatey\logs\maintenance.log"
$maxAge = (Get-Date).AddDays(-7)
if (Test-Path $logPath) {
$lastWrite = (Get-Item $logPath).LastWriteTime
if ($lastWrite -gt $maxAge) {
Write-Output "Maintenance completed within 7 days"
exit 0
}
}
Write-Output "Maintenance required"
exit 1
Configure Chocolatey global settings for enterprise use:
# Enterprise Configuration Script
choco config set cacheLocation "$env:ProgramData\chocolatey\cache"
choco config set commandExecutionTimeoutSeconds 14400
choco config set containsLegacyPackageInstalls true
choco config set maxDownloadRateBitsPerSecond 10485760
choco feature enable -n allowGlobalConfirmation
choco feature enable -n logEnvironmentValues
choco feature disable -n showNonElevatedWarnings
Warning: Automatic updates can potentially break applications if package updates introduce breaking changes. Consider testing updates in a pilot group before deploying to production devices.
Set up monitoring for the maintenance tasks by creating a simple reporting script:
# Maintenance Reporting Script
$devices = Get-Content "devices.txt" # List of device names
$report = @()
foreach ($device in $devices) {
try {
$logPath = "\\$device\c$\ProgramData\chocolatey\logs\maintenance.log"
if (Test-Path $logPath) {
$lastRun = (Get-Item $logPath).LastWriteTime
$status = if ($lastRun -gt (Get-Date).AddDays(-7)) { "OK" } else { "Overdue" }
} else {
$status = "No Log"
$lastRun = "Never"
}
$report += [PSCustomObject]@{
Device = $device
LastMaintenance = $lastRun
Status = $status
}
} catch {
$report += [PSCustomObject]@{
Device = $device
LastMaintenance = "Error"
Status = "Unreachable"
}
}
}
$report | Export-Csv "ChocolateyMaintenanceReport.csv" -NoTypeInformation
$report | Format-Table -AutoSize
Verification: Check that the maintenance script runs successfully and creates log entries in $env:ProgramData\chocolatey\logs\maintenance.log.