49 lines
1.8 KiB
PowerShell
49 lines
1.8 KiB
PowerShell
|
# Script Name: windows-updater.ps1
|
||
|
# Beschreibung: Installiert Windows Updates
|
||
|
# Lauffähig unter Windows Server 2016, 2019 und 2022
|
||
|
# Aufruf: -
|
||
|
# Autor: Patrick Asmus
|
||
|
# Web: https://www.media-techport.de
|
||
|
# Git-Reposit.: https://git.media-techport.de/scriptos/windows-updater.git
|
||
|
# Version: 1.0
|
||
|
# Datum: 29.11.2023
|
||
|
# Modifikation: Initial
|
||
|
#####################################################
|
||
|
|
||
|
# Funktion zur Formatierung des Datums im deutschen Stil
|
||
|
function Format-DeutschesDatum ([datetime] $date) {
|
||
|
return $date.ToString("dd.MM.yyyy HH:mm:ss")
|
||
|
}
|
||
|
|
||
|
# Variablen
|
||
|
$LogPfad = "C:\logs\windows-updater.log"
|
||
|
$ModulVerzeichnis = Join-Path -Path $PSScriptRoot -ChildPath "Module"
|
||
|
$env:PSModulePath += ";$ModulVerzeichnis"
|
||
|
|
||
|
# Importiere das PSWindowsUpdate-Modul
|
||
|
Import-Module -Name "PSWindowsUpdate" -Verbose
|
||
|
|
||
|
# Überprüfe, ob das Modul erfolgreich geladen wurde
|
||
|
if (Get-Module -Name PSWindowsUpdate -ListAvailable) {
|
||
|
Write-Host "PSWindowsUpdate-Modul erfolgreich geladen."
|
||
|
|
||
|
# Installiere Windows-Updates und speichere die Ausgabe
|
||
|
$Ausgabe = Install-WindowsUpdate -MicrosoftUpdate -AcceptAll -AutoReboot
|
||
|
|
||
|
# Überprüfe, ob ein Neustart erforderlich ist
|
||
|
if ($Ausgabe.RestartRequired) {
|
||
|
Write-Host "Ein Neustart ist erforderlich."
|
||
|
} else {
|
||
|
Write-Host "Kein Neustart erforderlich."
|
||
|
}
|
||
|
|
||
|
# Gib die Ausgabe der Windows-Update-Installation in der Kommandozeile aus und schreibe sie ins Logfile
|
||
|
$Ausgabe | ForEach-Object {
|
||
|
$LogMeldung = "ComputerName: $($_.ComputerName) Result: $($_.Result) KB: $($_.KB) Size: $($_.Size) Title: $($_.Title)"
|
||
|
Write-Host $LogMeldung
|
||
|
$LogMeldung | Out-File -Append -FilePath $LogPfad
|
||
|
}
|
||
|
} else {
|
||
|
Write-Host "Fehler beim Laden des PSWindowsUpdate-Moduls."
|
||
|
}
|