PowerShell/scripts/install-updates.ps1

69 lines
2.0 KiB
PowerShell
Raw Normal View History

2024-10-01 15:11:03 +02:00
<#
2021-09-17 16:14:05 +02:00
.SYNOPSIS
Installs software updates
2021-09-17 16:14:05 +02:00
.DESCRIPTION
2024-01-24 13:16:24 +01:00
This PowerShell script installs software updates for the local machine (might need admin rights).
2024-12-04 11:30:18 +01:00
HINT: Use the script 'list-updates.ps1' to list the latest software updates in advance.
2021-09-17 16:14:05 +02:00
.EXAMPLE
2023-08-06 21:35:36 +02:00
PS> ./install-updates.ps1
2024-12-04 11:30:18 +01:00
(1/2) Checking update requirements...
Drive C: has 441 GB free (56% of 1TB used)
Swap space has 1GB free (2% of 1GB used)
No pending system reboot
2024-01-24 13:16:24 +01:00
(2/2) Installing updates from winget and Microsoft Store...
...
2021-09-17 16:14:05 +02:00
.LINK
https://github.com/fleschutz/PowerShell
2022-01-29 12:47:46 +01:00
.NOTES
2022-09-06 21:42:04 +02:00
Author: Markus Fleschutz | License: CC0
2021-09-17 16:14:05 +02:00
#>
try {
2024-01-24 13:16:24 +01:00
$stopWatch = [system.diagnostics.stopwatch]::startNew()
2021-09-17 16:14:05 +02:00
if ($IsLinux) {
2024-12-04 11:30:18 +01:00
"⏳ (1/5) Checking update requirements..."
& "$PSScriptRoot/check-smart-devices.ps1"
& "$PSScriptRoot/check-drive-space.ps1" /
2024-02-07 17:31:48 +01:00
& "$PSScriptRoot/check-swap-space.ps1"
& "$PSScriptRoot/check-pending-reboot.ps1"
Start-Sleep -seconds 3
""
"⏳ (2/5) Querying latest package information..."
& sudo apt update
2021-09-17 16:21:19 +02:00
"⏳ (3/5) Removing obsolete packages..."
2021-09-29 08:18:28 +02:00
& sudo apt autoremove --yes
2021-09-17 16:21:19 +02:00
"⏳ (4/5) Upgrading installed packages..."
& sudo apt upgrade --yes
"⏳ (5/5) Upgrading installed Snaps..."
2021-09-28 16:28:53 +02:00
& sudo snap refresh
2023-09-17 12:26:58 +02:00
} elseif ($IsMacOS) {
Write-Progress "⏳ Installing updates..."
& sudo softwareupdate -i -a
Write-Progress -completed " "
2021-09-17 16:14:05 +02:00
} else {
# Windows:
2024-12-04 11:30:18 +01:00
"⏳ (1/2) Checking update requirements..."
& "$PSScriptRoot/check-smart-devices.ps1"
2024-01-24 13:16:24 +01:00
& "$PSScriptRoot/check-drive-space.ps1" C
2024-02-07 17:31:48 +01:00
& "$PSScriptRoot/check-swap-space.ps1"
& "$PSScriptRoot/check-pending-reboot.ps1"
Start-Sleep -seconds 3
""
2024-12-04 11:30:18 +01:00
"⏳ (2/2) Installing updates from winget..."
2024-01-24 13:16:24 +01:00
""
2024-11-30 14:23:41 +01:00
& winget upgrade --all --source=winget
# & winget upgrade --all --source=msstore # does not work
2021-09-17 16:14:05 +02:00
}
2024-01-24 13:16:24 +01:00
[int]$elapsed = $stopWatch.Elapsed.TotalSeconds
2024-05-14 21:33:14 +02:00
"✅ Updates installed in $($elapsed)s."
2021-09-27 10:09:45 +02:00
exit 0 # success
2021-09-17 16:14:05 +02:00
} catch {
2022-04-13 12:06:32 +02:00
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
2021-09-17 16:14:05 +02:00
exit 1
2023-08-06 21:35:36 +02:00
}