# Erstellen einer temporären Datei zur Protokollierung von Nachrichten $logFilePath = Join-Path -Path $env:TEMP -ChildPath "D4dPostgreSQLUninstallPowerShell.log" $logFile = New-Item -Path $logFilePath -ItemType File # Funktion zum Protokollieren von Nachrichten in die Datei function LogMessage($message) { $message | Out-File -FilePath $logFilePath -Append } function DeletePostgreSQL { # Protokollieren der Startnachricht LogMessage "Deinstalliere PostgreSQL..." $isAdmin = ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator) if ($isAdmin) { LogMessage "Skript wird mit Administratorrechten ausgeführt." } else { LogMessage "Skript wird ohne Administratorrechte ausgeführt." return; } # Suche nach dem Wert der AppPath-Variable unter HKEY_CURRENT_USER\Software\MMI\data4doc $AppPathValue = Get-ItemPropertyValue 'HKCU:\Software\MMI\data4doc' 'AppPath' if (-not (Test-Path $AppPathValue)) { LogMessage "Die d4d Pfad wurde nicht gefunden." exit } # Pfad zur UninstallPostgreSQL.exe-Datei $uninstallPath = "UninstallPostgresql.exe" $UninstallExe = Join-Path -Path $AppPathValue -ChildPath "uninstall\UninstallPostgresql.exe" # Überprüfen, ob die Datei vorhanden ist if (-not (Test-Path $UninstallExe)) { LogMessage "Die $(UninstallExe) wurde nicht gefunden." exit } # Ausführen der Datei mit Administratorrechten im versteckten Modus $processStartInfo = New-Object System.Diagnostics.ProcessStartInfo $processStartInfo.FileName = $UninstallExe $processStartInfo.Verb = "runas" $processStartInfo.WindowStyle = "Normal" $process = [System.Diagnostics.Process]::Start($processStartInfo) $process.WaitForExit() # Überprüfen und Entfernen des Schlüssels HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\MMI-data4doc-PostgreSQL if (Test-Path 'HKLM:\SYSTEM\CurrentControlSet\Services\MMI-data4doc-PostgreSQL') { Remove-Item -Path 'HKLM:\SYSTEM\CurrentControlSet\Services\MMI-data4doc-PostgreSQL' -Force LogMessage "Schlüssel HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\MMI-data4doc-PostgreSQL entfernt." } # Datei UninstallPostgreSQL.exe löschen if ($process.ExitCode -eq 0) { Remove-Item -Path $UninstallExe -Force LogMessage"Die Datei UninstallPostgreSQL.exe wurde erfolgreich gelöscht." } # Protokollieren der Abschlussnachricht LogMessage "PostgreSQL-Deinstallation abgeschlossen." } # Aufruf der Funktion DeletePostgreSQL DeletePostgreSQL # Regex pattern for SIDs $PatternSID = 'S-1-5-21-\d+-\d+\-\d+\-\d+$' LogMessage "Lösche d4d Registry-Eiträge" # Get Username, SID, and location of ntuser.dat for all users $ProfileList = gp 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\*' | Where-Object {$_.PSChildName -match $PatternSID} | Select @{name="SID";expression={$_.PSChildName}}, @{name="UserHive";expression={"$($_.ProfileImagePath)\ntuser.dat"}}, @{name="Username";expression={$_.ProfileImagePath -replace '^(.*[\\\/])', ''}} # Get all user SIDs found in HKEY_USERS (ntuder.dat files that are loaded) $LoadedHives = gci Registry::HKEY_USERS | ? {$_.PSChildname -match $PatternSID} | Select @{name="SID";expression={$_.PSChildName}} # Get all users that are not currently logged $UnloadedHives = Compare-Object $ProfileList.SID $LoadedHives.SID | Select @{name="SID";expression={$_.InputObject}}, UserHive, Username # Loop through each profile on the machine Foreach ($item in $ProfileList) { # Load User ntuser.dat if it's not already loaded IF ($item.SID -in $UnloadedHives.SID) { reg load HKU\$($Item.SID) $($Item.UserHive) | Out-Null } if(Test-Path("registry::HKEY_USERS\$($Item.SID)\Software\MMI")) { Remove-Item -Path Registry::HKEY_USERS\$($Item.SID)\Software\MMI\data4doc -Recurse $names = Get-ChildItem registry::HKEY_USERS\$($Item.SID)\Software\MMI if ($names -eq $null) { Remove-Item -Path Registry::HKEY_USERS\$($Item.SID)\Software\MMI -Recurse } else { $names.Handle.Close() } } if(Test-Path("registry::HKEY_USERS\$($Item.SID)_Classes")) { if((Test-Path("registry::HKEY_USERS\$($Item.SID)_Classes\data4doc"))) { Remove-Item -Path Registry::HKEY_USERS\$($Item.SID)_Classes\data4doc -Recurse } } ##################################################################### # Unload ntuser.dat IF ($item.SID -in $UnloadedHives.SID) { ### Garbage collection and closing of ntuser.dat ### [gc]::Collect() Start-Sleep -Seconds 2 Start-Process -FilePath "$Env:SystemRoot\system32\CMD.EXE" -ArgumentList "/C REG UNLOAD HKU\$($Item.SID)" -Wait -WindowStyle Hidden #reg unload HKU\$($Item.SID) | Out-Null } } Foreach ($item in $ProfileList) { # Load User ntuser.dat if it's not already loaded IF ($item.SID -in $UnloadedHives.SID) { reg load HKU\$($Item.SID)_Classes $($Item.UserHive) | Out-Null } ##################################################################### # This is where you can read/modify a users portion of the registry if(Test-Path("registry::HKEY_USERS\$($Item.SID)_Classes")) { if(Test-Path("registry::HKEY_USERS\$($Item.SID)_Classes\data4doc")) { Remove-Item -Path Registry::HKEY_USERS\$($Item.SID)_Classes\data4doc -Recurse } } ##################################################################### # Unload ntuser.dat IF ($item.SID -in $UnloadedHives.SID) { ### Garbage collection and closing of ntuser.dat ### [gc]::Collect() Start-Sleep -Seconds 2 Start-Process -FilePath "$Env:SystemRoot\system32\CMD.EXE" -ArgumentList "/C REG UNLOAD HKU\$($Item.SID)_Classes" -Wait -WindowStyle Hidden # reg unload HKU\$($Item.SID)_Classes | Out-Null } LogMessage "Löschen d4d Registry-Einträge abgeschlossen." }