diff --git a/windows-updater.ps1 b/windows-updater.ps1 index 1146a9c..795bd01 100644 --- a/windows-updater.ps1 +++ b/windows-updater.ps1 @@ -1,48 +1,60 @@ -# 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." -} +# 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 "Modules" +$env:PSModulePath += ";$ModulVerzeichnis" + +# Zeitstempel für das Logfile +$LogZeitstempel = Format-DeutschesDatum (Get-Date) +$LogEintrag = "Script gestartet am: $LogZeitstempel" +Write-Host $LogEintrag +$LogEintrag | Out-File -Append -FilePath $LogPfad + +# 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." +} + +# Zeitstempel für das Logfile nach Abschluss des Skripts +$LogZeitstempel = Format-DeutschesDatum (Get-Date) +$LogEintrag = "Script beendet am: $LogZeitstempel" +Write-Host $LogEintrag +$LogEintrag | Out-File -Append -FilePath $LogPfad