Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Datum für Backup-Dateien
- $timestamp = Get-Date -Format "yyyyMMdd_HHmmss"
- $backupPath = "C:\Windows\Temp\WSUS_Backup_$timestamp"
- # Funktion für Error-Logging
- function Write-LogError {
- param($Message)
- Write-Host "ERROR: $Message" -ForegroundColor Red
- Add-Content -Path "$backupPath\error.log" -Value "$(Get-Date -Format 'yyyy-MM-dd HH:mm:ss'): $Message"
- }
- try {
- # 1. Backup-Verzeichnis erstellen
- Write-Host "Erstelle Backup-Verzeichnis..." -ForegroundColor Green
- New-Item -ItemType Directory -Path $backupPath -Force
- # 2. Registry-Einträge sichern
- Write-Host "Sichere Registry-Einträge..." -ForegroundColor Green
- $wsusKey = "HKLM:\SOFTWARE\Policies\Microsoft\Windows\WindowsUpdate"
- $auKey = "HKLM:\SOFTWARE\Policies\Microsoft\Windows\WindowsUpdate\AU"
- if (Test-Path $wsusKey) {
- $wsusSettings = Get-ItemProperty -Path $wsusKey
- $wsusSettings | Export-Clixml -Path "$backupPath\wsus_settings.xml"
- }
- if (Test-Path $auKey) {
- $auSettings = Get-ItemProperty -Path $auKey
- $auSettings | Export-Clixml -Path "$backupPath\au_settings.xml"
- }
- # 3. Dienste stoppen
- Write-Host "Stoppe Dienste..." -ForegroundColor Yellow
- Stop-Service wuauserv -Force -ErrorAction Stop
- Stop-Service bits -Force -ErrorAction Stop
- # 4. Registry-Schlüssel löschen
- Write-Host "Lösche WSUS Registry-Einträge..." -ForegroundColor Yellow
- if (Test-Path $wsusKey) {
- Remove-Item -Path $wsusKey -Recurse -Force
- }
- # 5. SoftwareDistribution-Ordner sichern
- Write-Host "Sichere SoftwareDistribution-Ordner..." -ForegroundColor Yellow
- if (Test-Path "C:\Windows\SoftwareDistribution") {
- Rename-Item -Path "C:\Windows\SoftwareDistribution" -NewName "SoftwareDistribution.old_$timestamp" -Force
- }
- # 6. Dienste wieder starten
- Write-Host "Starte Dienste neu..." -ForegroundColor Yellow
- Start-Service bits
- Start-Service wuauserv
- Write-Host "`nBackup wurde erstellt in: $backupPath" -ForegroundColor Green
- Write-Host "Bitte führen Sie jetzt Windows Update aus.`n" -ForegroundColor Yellow
- Write-Host "Zum Wiederherstellen der WSUS-Einstellungen, führen Sie das Restore-Script aus." -ForegroundColor Yellow
- # Erstelle Restore-Script
- $restoreScript = @"
- # Restore-Script für WSUS-Einstellungen
- try {
- # 1. Dienste stoppen
- Stop-Service wuauserv -Force
- Stop-Service bits -Force
- # 2. WSUS-Einstellungen wiederherstellen
- New-Item -Path "$wsusKey" -Force
- Set-ItemProperty -Path "$wsusKey" -Name "WUServer" -Value "$($wsusSettings.WUServer)"
- Set-ItemProperty -Path "$wsusKey" -Name "WUStatusServer" -Value "$($wsusSettings.WUStatusServer)"
- New-Item -Path "$auKey" -Force
- Set-ItemProperty -Path "$auKey" -Name "UseWUServer" -Value 1
- # 3. Dienste neu starten
- Start-Service bits
- Start-Service wuauserv
- Write-Host "WSUS-Einstellungen wurden wiederhergestellt." -ForegroundColor Green
- }
- catch {
- Write-Host "Fehler beim Wiederherstellen: `$_" -ForegroundColor Red
- }
- "@
- $restoreScript | Out-File "$backupPath\Restore_WSUS.ps1"
- }
- catch {
- Write-LogError $_.Exception.Message
- Write-Host "`nFehler aufgetreten. Überprüfen Sie die Logdatei: $backupPath\error.log" -ForegroundColor Red
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement