Patching a handful of servers by hand is manageable. Patching hundreds of Windows and Linux machines spread across several client networks is a different job, and doing it manually means late nights, missed machines and no record of what was installed where.
This guide shows how to automate operating system patching on Windows and Linux with free tools and a few short scripts, then report the results to a ticketing system so failures get followed up. The examples use PSWindowsUpdate on Windows, unattended-upgrades on Ubuntu and Debian, and dnf-automatic on RHEL, Rocky Linux and AlmaLinux.
The same approach works for an internal IT team or for a managed service provider looking after several clients. Run the scripts per client, and keep the reporting step pointed at one ticket queue.
Prerequisites
- Administrator access on the Windows machines, with PowerShell 5.1 or later
- sudo access on the Linux machines
- Outbound HTTPS from each machine to your ticketing system’s API
- An API token for the ticketing system, stored as a protected environment variable or in a secrets store, never in the script itself
jqinstalled on the Linux machines for building JSON payloads
Windows: PSWindowsUpdate in a scheduled task
PSWindowsUpdate is a widely used community PowerShell module that wraps the Windows Update API. Install it from the PowerShell Gallery. On Windows PowerShell 5.1, turn on TLS 1.2 first, and register the Microsoft Update service if it is not already registered:
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
Install-Module -Name PSWindowsUpdate -Force
Add-WUServiceManager -MicrosoftUpdate -Confirm:$false
Get-WindowsUpdate -MicrosoftUpdate
The second command lists pending updates without installing them. Use it to confirm the module works before scheduling anything.
Create the patch script at C:\Scripts\Patch-Windows.ps1:
Import-Module PSWindowsUpdate
$logDir = "C:\PatchLogs"
New-Item -ItemType Directory -Path $logDir -Force | Out-Null
$stamp = Get-Date -Format "yyyy-MM-dd_HHmm"
$result = Install-WindowsUpdate -MicrosoftUpdate -AcceptAll -IgnoreReboot -Confirm:$false
$result | Select-Object KB, Title, Result | Sort-Object KB -Unique | Export-Csv "$logDir\patch_$stamp.csv" -NoTypeInformation
$failed = $result | Where-Object { $_.Result -eq "Failed" }
if ($failed) { exit 1 } else { exit 0 }
Register it as a weekly scheduled task that runs as SYSTEM:
$action = New-ScheduledTaskAction -Execute "powershell.exe" -Argument "-NoProfile -ExecutionPolicy Bypass -File C:\Scripts\Patch-Windows.ps1"
$trigger = New-ScheduledTaskTrigger -Weekly -DaysOfWeek Wednesday -At 2am
Register-ScheduledTask -TaskName "Weekly Patching" -Action $action -Trigger $trigger -User "SYSTEM" -RunLevel Highest
Test the script interactively once before scheduling it, since the output columns can vary between module versions. The script uses -IgnoreReboot so machines do not restart in the middle of a client’s working day. Schedule reboots separately, or let users restart within a grace period set by Group Policy.
Linux: unattended-upgrades and dnf-automatic
Ubuntu and Debian
Install and enable unattended-upgrades:
sudo apt update
sudo apt install unattended-upgrades -y
sudo dpkg-reconfigure --priority=low unattended-upgrades
Edit /etc/apt/apt.conf.d/50unattended-upgrades to control reboots:
Unattended-Upgrade::Automatic-Reboot "true";
Unattended-Upgrade::Automatic-Reboot-Time "02:00";
Test the configuration without installing anything:
sudo unattended-upgrade --dry-run --debug
Results are logged to /var/log/unattended-upgrades/unattended-upgrades.log.
RHEL, Rocky Linux and AlmaLinux 8 and 9
Install dnf-automatic:
sudo dnf install dnf-automatic -y
Edit /etc/dnf/automatic.conf and set:
[commands]
upgrade_type = security
apply_updates = yes
Enable the timer and confirm it is scheduled:
sudo systemctl enable --now dnf-automatic.timer
systemctl list-timers dnf-automatic.timer
upgrade_type = security limits automatic installs to security updates, which is a sensible default for production servers. Bug-fix and enhancement updates can then go through a manual maintenance window. Fedora and other dnf5 systems use dnf5-automatic with a different config path.
Log results to your ticketing system
Once the scheduled jobs run, send each result to your ticketing system through its API. A small wrapper script can post the hostname, the list of installed updates and the exit code as a ticket note, and open a new ticket when the exit code is not zero.
Most PSA software for MSPs accepts these API calls and ties each ticket to the client’s contract, so failed patch runs land in the same queue as user requests.
The endpoint path and payload fields differ between products, so treat the URLs below as placeholders and check your ticketing system’s API documentation. The token has to be available to the scheduled job: set it as a machine-level environment variable on Windows, and through EnvironmentFile= in a systemd unit or a sourced file on Linux.
On Linux, a wrapper at /usr/local/bin/patch-report.sh:
#!/usr/bin/env bash
set -euo pipefail
HOST=$(hostname -f 2>/dev/null || hostname)
LOG=/var/log/unattended-upgrades/unattended-upgrades.log
STATUS="ok"
if [[ ! -f "$LOG" ]]; then
STATUS="failed"; SUMMARY="log file not found"
else
if grep -q "ERROR" <(tail -n 200 "$LOG"); then STATUS="failed"; fi
SUMMARY=$(tail -n 20 "$LOG")
fi
jq -n --arg host "$HOST" --arg status "$STATUS" --arg summary "$SUMMARY" \
'{device: $host, status: $status, note: $summary}' |
curl -sS --fail-with-body -X POST "https://psa.example.com/api/patch-results" \
-H "Authorization: Bearer ${PSA_TOKEN}" \
-H "Content-Type: application/json" \
--data @-
The grep check is rough: it can match an error from an earlier run that is still in the last 200 lines. Run the wrapper from cron or a systemd timer about an hour after the patch window.
On RHEL-based systems, replace the LOG and grep lines with a journal check, for example SUMMARY=$(journalctl -u dnf-automatic.service --since "-24h" --no-pager | tail -n 20).
On Windows, add these lines to the end of Patch-Windows.ps1, before the exit:
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$body = @{
device = $env:COMPUTERNAME
status = $(if ($failed) { "failed" } else { "ok" })
note = ($result | Select-Object KB, Result | Out-String)
} | ConvertTo-Json
Invoke-RestMethod -Uri "https://psa.example.com/api/patch-results" -Method Post `
-Headers @{ Authorization = "Bearer $env:PSA_TOKEN" } -ContentType "application/json" -Body $body
Use the failed status in your ticketing system’s automation rules to open a ticket for the client and assign it to the on-call technician.
Verify the runs
Check three things after the first scheduled run:
- The jobs ran. On Windows, open Task Scheduler and check the last run result of “Weekly Patching”. On Linux, run
systemctl list-timersor check the unattended-upgrades log timestamp. - Updates installed. Compare the CSV or log output with
Get-WindowsUpdateon Windows,apt list --upgradableon Ubuntu anddnf updateinfo list --securityon RHEL to confirm no security updates are left pending. - Reports arrived. Confirm a note or ticket appeared in the ticketing system for every machine, including the ones that succeeded.
Machines that never report back are the ones to chase first. They are usually offline, out of disk space, or blocked from reaching the API.
Conclusion
With PSWindowsUpdate, unattended-upgrades and dnf-automatic doing the installs, and a short wrapper script reporting each result, patching across several client networks becomes a scheduled job with a paper trail. Failed runs turn into tickets automatically, and the weekly patch window runs without someone watching it.