PowerShell安装Oracle、PostgreSQL及检测Windows系统安装状态求助
Hey there! Let's tackle your PowerShell database needs step by step—since you're new to PowerShell, I'll keep things straightforward and actionable 😊
First, let's cover how to check if SQL Server, Oracle, and PostgreSQL are already installed on your Windows machine.
1.1 SQL Server Detection
There are a couple reliable ways to check for SQL Server:
- Registry Check: This is fast and targets SQL Server's instance registry entries
# List all installed SQL Server instances Get-ItemProperty "HKLM:\Software\Microsoft\Microsoft SQL Server\Instance Names\SQL" -ErrorAction SilentlyContinue | Select-Object -ExpandProperty "(default)" - Installed Programs List: If you want more details like version, use this (note:
Win32_Productcan be slow as it validates MSI packages)Get-CimInstance -ClassName Win32_Product | Where-Object {$_.Name -like "*SQL Server*"} | Select-Object Name, Version, InstallLocation
1.2 Oracle Detection (Your Working Command, Formalized)
Since you already have this working, here's a polished version for reference that pulls key details:
# Check Oracle installations and their home directories Get-ChildItem "HKLM:\Software\Oracle\KEY*" -ErrorAction SilentlyContinue | ForEach-Object { Get-ItemProperty $_.PSPath | Select-Object @{Name="Oracle Home"; Expression={$_.Oracle_Home}}, Version }
1.3 PostgreSQL Detection (The Fix You Need)
The reason your Oracle command fails for PostgreSQL is that PostgreSQL stores installation data in different registry paths. Try these methods:
- Registry Check (Covers 64-bit and 32-bit):
# Check both 64-bit and 32-bit registry paths for PostgreSQL $pgRegistryPaths = @("HKLM:\Software\PostgreSQL", "HKLM:\Software\Wow6432Node\PostgreSQL") foreach ($path in $pgRegistryPaths) { if (Test-Path $path) { Get-ChildItem $path | ForEach-Object { Get-ItemProperty $_.PSPath | Select-Object @{Name="Version"; Expression={$_.PSChildName}}, BaseDirectory, ServiceName } } } - Service Check: PostgreSQL runs a named service (usually
postgresql-x64-XXwhere XX is the version)# List running or stopped PostgreSQL services Get-Service -Name "postgresql*" -ErrorAction SilentlyContinue | Select-Object Name, Status, DisplayName - Installed Programs List:
Get-CimInstance -ClassName Win32_Product | Where-Object {$_.Name -like "*PostgreSQL*"} | Select-Object Name, Version, InstallLocation
Now let's walk through silent installations for both databases—this is the most efficient way to install via PowerShell.
2.1 Oracle Installation
Oracle requires a response file for silent installs (to avoid interactive prompts):
- Download the Oracle installation media (zip files) and extract to a folder (e.g.,
C:\OracleInstall) - Generate a response file by running
setup.exe -generateResponseFileand configure it with your settings (edition, install path, passwords, etc.) - Run the silent install with PowerShell:
# Silent Oracle install using response file Start-Process -FilePath "C:\OracleInstall\setup.exe" -ArgumentList "-silent -responseFile C:\OracleInstall\your-response-file.rsp" -Wait -NoNewWindow
2.2 PostgreSQL Installation
PostgreSQL's installer supports direct command-line arguments for silent installs:
- Download the PostgreSQL installer (e.g.,
postgresql-16.2-1-windows-x64.exe) - Run this PowerShell command (adjust paths and settings to your needs):
Key arguments explained:# Silent PostgreSQL install example Start-Process -FilePath "C:\Downloads\postgresql-16.2-1-windows-x64.exe" -ArgumentList ` "--mode unattended", ` "--superpassword YourStrongPassword123", ` "--install_dir C:\Program Files\PostgreSQL\16", ` "--serverport 5432", ` "--enable-components server,pgAdmin4" ` -Wait -NoNewWindow--mode unattended: Runs the install without prompts--superpassword: Sets the password for the defaultpostgresadmin user--install_dir: Defines where PostgreSQL will be installed--enable-components: Choose which parts to install (server, pgAdmin, etc.)
内容的提问来源于stack exchange,提问作者Prajwal

