SQLServerCentral Logo SQLServerCentral Logo
  • Articles
  • Editorials
  • Stairways
  • Forums
    • Forums home
    • Active threads
    • Latest topics
    • Most popular
    • Leaderboard
  • Scripts
  • QotD
  • Books
  • Blogs
  • Register
  • Login
  • Write for us
  • Menu
  • Articles
  • Editorials
  • Stairways
  • Forums
    • Forums home
    • Active threads
    • Latest topics
    • Most popular
    • Leaderboard
  • Scripts
  • QotD
  • Books
  • Blogs
  • Write for us
  • Register
  • Login

sqlks

  • Profile
  • Topics Started
  • Replies Created
  • Leaderboards

sqlks

Account

  • Last login: October 10th 2025
  • Login count: 395
  • Reputation: Old Hand
  • Points: 378

About

  • Description:

    # --- Initialization ---
    $Timestamp = Get-Date -Format "yyyyMMdd_HHmmss"
    $LogDir = "C:\Temp"
    if (-not (Test-Path $LogDir)) { New-Item -ItemType Directory -Path $LogDir -Force | Out-Null }
    $LogFile = Join-Path $LogDir "OracleClient_Cleanup_$Timestamp.log"

    $RegistryPaths = @(
    "HKLM:\SOFTWARE\ORACLE",
    "HKLM:\SOFTWARE\WOW6432Node\ORACLE"
    )

    Write-Host "Oracle Client Cleanup Utility" -ForegroundColor Cyan
    Write-Host "Log file: $LogFile" -ForegroundColor Yellow
    Add-Content -Path $LogFile -Value "Oracle Client Cleanup Log - $(Get-Date)"
    Add-Content -Path $LogFile -Value "====================================================="

    $PreviewItems = @()

    # --- Detect Oracle Registry Keys ---
    foreach ($RegPath in $RegistryPaths) {
    if (Test-Path $RegPath) {
    Write-Host "`nScanning registry path: $RegPath" -ForegroundColor Cyan
    $keys = Get-ChildItem $RegPath -ErrorAction SilentlyContinue

    foreach ($Key in $keys) {
    $oracleHome = (Get-ItemProperty -Path $Key.PSPath -ErrorAction SilentlyContinue).ORACLE_HOME
    $version = (Get-ItemProperty -Path $Key.PSPath -ErrorAction SilentlyContinue).VERSION

    if ($oracleHome) {
    $keyName = $Key.PSChildName

    Write-Host "`nFound registry key: $keyName" -ForegroundColor Yellow
    Write-Host "Oracle Home: $oracleHome"
    Write-Host "Version: $version"

    Add-Content -Path $LogFile -Value "`nRegistry Key: $keyName"
    Add-Content -Path $LogFile -Value "Oracle Home: $oracleHome"
    Add-Content -Path $LogFile -Value "Version: $version"

    if (Test-Path $oracleHome) {
    $deinstallExe = Join-Path $oracleHome "oui\bin\setup.exe"
    $inventory = "C:\Program Files\Oracle\Inventory\ContentsXML\inventory.xml"

    if (Test-Path $deinstallExe -and (Test-Path $inventory)) {
    Write-Host "Running Oracle deinstaller..." -ForegroundColor Cyan
    $cmd = "`"$deinstallExe`" -deinstall -home `"$oracleHome`""
    Write-Host "Executing: $cmd"
    Add-Content -Path $LogFile -Value "Executing: $cmd"
    cmd.exe /c $cmd
    Add-Content -Path $LogFile -Value "Deinstall attempted via OUI for $oracleHome"
    } else {
    Write-Host "Inventory or setup.exe missing — manual cleanup needed for $oracleHome" -ForegroundColor Yellow
    Add-Content -Path $LogFile -Value "Inventory missing — manual cleanup required for $oracleHome"

    $PreviewItems += [PSCustomObject]@{
    Type = "Folder"
    Path = $oracleHome
    Action = "Delete"
    }
    $PreviewItems += [PSCustomObject]@{
    Type = "RegistryKey"
    Path = $Key.PSPath
    Action = "Delete"
    }
    }
    } else {
    $PreviewItems += [PSCustomObject]@{
    Type = "RegistryKey"
    Path = $Key.PSPath
    Action = "Delete (Orphaned)"
    }
    }
    } else {
    Add-Content -Path $LogFile -Value "Skipped unreadable or invalid registry key: $($Key.PSPath)"
    }
    }
    }
    }

    # --- Add environment variables and PATH changes to preview ---
    $envVars = @("ORACLE_HOME","TNS_ADMIN","ORACLE_SID")
    foreach ($var in $envVars) {
    if (Test-Path "Env:\$var") {
    $PreviewItems += [PSCustomObject]@{
    Type = "EnvironmentVariable"
    Path = $var
    Action = "Remove"
    }
    }
    }

    $path = [Environment]::GetEnvironmentVariable("Path","Machine")
    if ($path -match "oracle") {
    $PreviewItems += [PSCustomObject]@{
    Type = "PATH"
    Path = "System PATH contains Oracle entries"
    Action = "Clean"
    }
    }

    # --- Preview Section ---
    if ($PreviewItems.Count -gt 0) {
    Write-Host "`nThe following items will be deleted or modified:" -ForegroundColor Cyan
    $PreviewItems | Format-Table -AutoSize

    $confirm = Read-Host "`nDo you want to proceed with cleanup? (Y/N)"
    if ($confirm -match "^[Yy]$") {
    Write-Host "`nStarting cleanup..." -ForegroundColor Green
    Add-Content -Path $LogFile -Value "`nCleanup confirmed by user."

    foreach ($item in $PreviewItems) {
    switch ($item.Type) {
    "Folder" {
    Write-Host "Deleting folder: $($item.Path)" -ForegroundColor Yellow
    try {
    Remove-Item -Recurse -Force $item.Path -ErrorAction Stop
    Add-Content -Path $LogFile -Value "Deleted folder: $($item.Path)"
    } catch {
    $msg = "Failed to delete folder $($item.Path): $($_.Exception.Message)"
    Write-Host $msg -ForegroundColor Red
    Add-Content -Path $LogFile -Value $msg
    }
    }
    "RegistryKey" {
    Write-Host "Removing registry key: $($item.Path)" -ForegroundColor Yellow
    try {
    Remove-Item -Recurse -Force $item.Path -ErrorAction Stop
    Add-Content -Path $LogFile -Value "Removed registry key: $($item.Path)"
    } catch {
    $msg = "Failed to remove registry key $($item.Path): $($_.Exception.Message)"
    Write-Host $msg -ForegroundColor Red
    Add-Content -Path $LogFile -Value $msg
    }
    }
    "EnvironmentVariable" {
    Write-Host "Removing environment variable: $($item.Path)" -ForegroundColor Yellow
    [Environment]::SetEnvironmentVariable($item.Path, $null, [EnvironmentVariableTarget]::Machine)
    [Environment]::SetEnvironmentVariable($item.Path, $null, [EnvironmentVariableTarget]::User)
    Add-Content -Path $LogFile -Value "Removed environment variable: $($item.Path)"
    }
    "PATH" {
    Write-Host "Cleaning Oracle entries from PATH..." -ForegroundColor Yellow
    $newPath = ($path -split ";") | Where-Object {$_ -notmatch "oracle"} -join ";"
    [Environment]::SetEnvironmentVariable("Path",$newPath,[EnvironmentVariableTarget]::Machine)
    Add-Content -Path $LogFile -Value "Removed Oracle entries from PATH"
    }
    }
    }
    } else {
    Write-Host "`nCleanup aborted by user. No changes made." -ForegroundColor Red
    Add-Content -Path $LogFile -Value "`nCleanup aborted by user. No changes made."
    return
    }
    } else {
    Write-Host "`nNo Oracle components found for cleanup." -ForegroundColor Green
    Add-Content -Path $LogFile -Value "`nNo Oracle components found for cleanup."
    }

    Write-Host "`nCleanup complete. Review log file at: $LogFile" -ForegroundColor Green
    Add-Content -Path $LogFile -Value "`nCleanup complete. $(Get-Date)"

Forum

  • Forum role: Participant
  • Forum topics: 2
  • Forum replies: 1
  • About SQLServerCentral
  • Contact Us
  • Terms of Use
  • Privacy Policy
  • Contribute
  • Contributors
  • Authors
  • Newsletters
Redgate logo