1-Klick-Update unter Windows mit der PowerShell und Chocolatey

Letzte Änderung: 28.03.2017 - 19:50 Uhr

Auf meinen Windows-Systemen verwalte ich die meiste Software mit dem Paketmanager Chocolatey (sehr zu empfehlen). Nun dachte ich mir, ich automatisiere die Suche nach Windows Updates sowie aktualisierter Software über ein Skript. So gibt es praktisch ein 1-Klick-Update für das gesamte System. Herausgekommen ist ein relativ simples PowerShell-Skript.

Benötigt wird das Modul Windows Update Powershell Module. Das wird unter C:\Users\USERNAME\Documents\WindowsPowerShell\Modules abgelegt. Ebenfalls muss natürlich Chocolatey auf dem System installiert sein.

Das Skript sucht erst nach Windows Updates. Findet es welche, werden sie angezeigt und der Nutzer wird gefragt, ob sie installiert werden sollen. Gleiches passiert danach mit Softwareupdates über Chocolatey. Zum Schluss wird noch geprüft, ob ein Reboot des Systems nötig ist und ggf. gefragt, ob dieser gleich durchgeführt werden soll.

Da dieses Skript mit Admin-Rechten laufen muss, ist es am einfachsten, eine bat-Datei zu erzeugen, die das Skript aufruft (für Hinweise auf einen besseren Weg wäre ich dankbar):

@echo off
powershell.exe -Command "C:\Users\USERNAME\Documents\WindowsPowerShell\do-full-upgrade.ps1"

Auf die bat-Datei kann eine Verknüpfung erstellt werden und in den Eigenschaften dieser Verknüpfung kann dann unter "Erweitert" festgelegt werden, dass sie als Administrator ausgeführt werden soll. Diese Verknüpfung kann dann ins Startmenü oder sonst wo hin - und dann startet man mit einem Klick darauf das Komplett-Update, inkl. UAC-Abfrage.

In diesem ZIP sind alle notwendigen Files, nur der Pfad in der Datei do-full-upgrade.bat und in der Verknüpfung müssen angepasst werden. Das ZIP einfach nach C:\Users\USERNAME\Documents\WindowsPowerShell entpacken, die bat-Datei anpassen und alles sollte funktionieren:

do-full-upgrade.zip

[Kleiner Nachtrag: Ggf. muss den Skripten die Ausführung erlaubt werden, je nach lokaler Sicherheheitseinstellung (Get-ExecutionPolicy). Das betrifft nicht nur das PowerShell-Skript selbst, sondern auch das Windows Update-Modul.]

Das Ergebnis sieht dann in etwa so aus:

Ausgabe des Skripts

Und hier ist das eigentliche PowerShell-Skript:

#Requires -Version 3.0
#Requires -RunAsAdministrator

<# .SYNOPSIS
   Checks for updates on the local machine.
   .DESCRIPTION
   Looks for Windows Updates and software updates via Chocolatey. If a reboot is necessary,
   it will ask to do so.
   .NOTES
   Needs "Windows Update Powershell Module" from https://gallery.technet.microsoft.com/scriptcenter/2d191bcd-3308-4edd-9de2-88dff796b0bc/
    Copy it to C:\Users\USER\Documents\WindowsPowerShell\Modules\PSWindowsUpdate

    Also needs installed Chocolatey of course -- https://chocolatey.org

    Has to be executed with admin rights. Best is to create a bat-File like this:
      @echo off
      powershell.exe -Command "\path\to\do-full-upgrade.ps1"
    and use a shortcut with "execute as admin" enabled
.LINK
    Benjamin Heil, kontakt@bheil.net
    https://www.bheil.net
#>

Import-Module PSWindowsUpdate

# Windows Update
Write-Host -BackgroundColor Magenta -ForegroundColor White ">>> WINDOWS UPDATE"
Write-Host "Checking for Windows Updates."
Write-Host -ForegroundColor DarkGray "This will take a while ..."
$updates = Get-WUInstall -ListOnly
if ($updates) {
    Write-Host -ForegroundColor Yellow "Updates found:"
    Write-Host ($updates | Format-Table | Out-String)
    $confirmation = Read-Host "Install all? [y/n]"
    if ($confirmation -eq 'y') {
        Get-WUInstall -AcceptAll -IgnoreReboot -Verbose
    }
} else {
    Write-Host -ForegroundColor Green "No Windows Updates available!"
}
Write-Host

# Chocolatey
Write-Host -BackgroundColor Blue -ForegroundColor White ">>> CHOCOLATEY"
Write-Host "Checking for outdated software packages via Chocolatey ..."
$choco = choco outdated
if ($choco -like "*Chocolatey has determined 0 package(s) are outdated*") {
    Write-Host -ForegroundColor Green "No updates available via Chocolatey"
} else {
    Write-Host -ForegroundColor DarkGray ($choco | Format-Table | Out-String)
    Write-Host -ForegroundColor Yellow "Outdated software found."
    $confirmation = Read-Host "Update all with Chocolatey? [y/n]"
    if ($confirmation -eq 'y') {
        cup all -y
    Write-Host -ForegroundColor Yellow "Press any key to continue ..."
    $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
    }
}
Write-Host

# Reboot
Write-Host -BackgroundColor DarkYellow -ForegroundColor White ">>> REBOOT STATUS"
Get-WURebootStatus