Advertisement
PiXLFAIL

WSUS_Temporaer_Deaktivieren_PS1

Dec 18th, 2024
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Datum für Backup-Dateien
  2. $timestamp = Get-Date -Format "yyyyMMdd_HHmmss"
  3. $backupPath = "C:\Windows\Temp\WSUS_Backup_$timestamp"
  4.  
  5. # Funktion für Error-Logging
  6. function Write-LogError {
  7.     param($Message)
  8.     Write-Host "ERROR: $Message" -ForegroundColor Red
  9.     Add-Content -Path "$backupPath\error.log" -Value "$(Get-Date -Format 'yyyy-MM-dd HH:mm:ss'): $Message"
  10. }
  11.  
  12. try {
  13.     # 1. Backup-Verzeichnis erstellen
  14.     Write-Host "Erstelle Backup-Verzeichnis..." -ForegroundColor Green
  15.     New-Item -ItemType Directory -Path $backupPath -Force
  16.  
  17.     # 2. Registry-Einträge sichern
  18.     Write-Host "Sichere Registry-Einträge..." -ForegroundColor Green
  19.     $wsusKey = "HKLM:\SOFTWARE\Policies\Microsoft\Windows\WindowsUpdate"
  20.     $auKey = "HKLM:\SOFTWARE\Policies\Microsoft\Windows\WindowsUpdate\AU"
  21.  
  22.     if (Test-Path $wsusKey) {
  23.         $wsusSettings = Get-ItemProperty -Path $wsusKey
  24.         $wsusSettings | Export-Clixml -Path "$backupPath\wsus_settings.xml"
  25.     }
  26.     if (Test-Path $auKey) {
  27.         $auSettings = Get-ItemProperty -Path $auKey
  28.         $auSettings | Export-Clixml -Path "$backupPath\au_settings.xml"
  29.     }
  30.  
  31.     # 3. Dienste stoppen
  32.     Write-Host "Stoppe Dienste..." -ForegroundColor Yellow
  33.     Stop-Service wuauserv -Force -ErrorAction Stop
  34.     Stop-Service bits -Force -ErrorAction Stop
  35.  
  36.     # 4. Registry-Schlüssel löschen
  37.     Write-Host "Lösche WSUS Registry-Einträge..." -ForegroundColor Yellow
  38.     if (Test-Path $wsusKey) {
  39.         Remove-Item -Path $wsusKey -Recurse -Force
  40.     }
  41.  
  42.     # 5. SoftwareDistribution-Ordner sichern
  43.     Write-Host "Sichere SoftwareDistribution-Ordner..." -ForegroundColor Yellow
  44.     if (Test-Path "C:\Windows\SoftwareDistribution") {
  45.         Rename-Item -Path "C:\Windows\SoftwareDistribution" -NewName "SoftwareDistribution.old_$timestamp" -Force
  46.     }
  47.  
  48.     # 6. Dienste wieder starten
  49.     Write-Host "Starte Dienste neu..." -ForegroundColor Yellow
  50.     Start-Service bits
  51.     Start-Service wuauserv
  52.  
  53.     Write-Host "`nBackup wurde erstellt in: $backupPath" -ForegroundColor Green
  54.     Write-Host "Bitte führen Sie jetzt Windows Update aus.`n" -ForegroundColor Yellow
  55.     Write-Host "Zum Wiederherstellen der WSUS-Einstellungen, führen Sie das Restore-Script aus." -ForegroundColor Yellow
  56.  
  57.     # Erstelle Restore-Script
  58.     $restoreScript = @"
  59. # Restore-Script für WSUS-Einstellungen
  60. try {
  61.    # 1. Dienste stoppen
  62.    Stop-Service wuauserv -Force
  63.    Stop-Service bits -Force
  64.  
  65.    # 2. WSUS-Einstellungen wiederherstellen
  66.    New-Item -Path "$wsusKey" -Force
  67.    Set-ItemProperty -Path "$wsusKey" -Name "WUServer" -Value "$($wsusSettings.WUServer)"
  68.    Set-ItemProperty -Path "$wsusKey" -Name "WUStatusServer" -Value "$($wsusSettings.WUStatusServer)"
  69.  
  70.    New-Item -Path "$auKey" -Force
  71.    Set-ItemProperty -Path "$auKey" -Name "UseWUServer" -Value 1
  72.  
  73.    # 3. Dienste neu starten
  74.    Start-Service bits
  75.    Start-Service wuauserv
  76.  
  77.    Write-Host "WSUS-Einstellungen wurden wiederhergestellt." -ForegroundColor Green
  78. }
  79. catch {
  80.    Write-Host "Fehler beim Wiederherstellen: `$_" -ForegroundColor Red
  81. }
  82. "@
  83.  
  84.     $restoreScript | Out-File "$backupPath\Restore_WSUS.ps1"
  85.  
  86. }
  87. catch {
  88.     Write-LogError $_.Exception.Message
  89.     Write-Host "`nFehler aufgetreten. Überprüfen Sie die Logdatei: $backupPath\error.log" -ForegroundColor Red
  90. }
  91.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement