Skip to main content

Find Database Type - Port - Remote Address.

With the below script Check-DatabaseStatus.ps1 you can executed and find what Database Type the Server is using also the Port where is listening and the Remote Address that is configure to listen.

🚀 One-Line Download & Execute :
Set-ExecutionPolicy -ExecutionPolicy Bypass -Scope Process -Force; Invoke-Expression (Invoke-WebRequest -Uri "https://docs.greenhome.stream/attachments/56" -UseBasicP) | Out-Null
✅ Script Features
  • Clears the screen.

  • Searches for MSSQL and MySQL services.

  • Checks default listening ports: 1433 (MSSQL) and 3306 (MySQL).

  • Displays results in color-coded output.

  • Shows a Windows message box with the final result.

📜 PowerShell Script

Save this script as Check-DatabaseStatus.ps1 and run it with PowerShell:

# Step 1: Clear Screenscreen
Clear-Host

# SetStep default2: databaseSetup infovariables
$foundDatabasesfoundDatabase = @()$null
$finalMessagelistenPort = ""$null
$remoteAddr = $null

# Function to write colored output
function Write-Color {
    param (
        [string]$Message,
        [ConsoleColor]$Color = "White"
    )
    Write-Host $Message -ForegroundColor $Color
}

# FunctionStep to3: checkSearch servicefor anddatabase port
function Check-Database {
    param (
        [string]$Name,
        [string]$ServiceNamePattern,
        [int]$DefaultPort
    )type
Write-Color "Searching for $NameDatabase Database.Type..." -Color DarkYellow

# Check for MSSQL
$servicemssqlService = Get-Service | Where-Object { $_.Name -like "*sql*" -and $ServiceNamePattern*_.Status -eq "Running" }

# Check for MySQL
$mysqlService = Get-Service | Where-Object { $_.Name -like "*mysql*" -and $_.Status -eq "Running" }

if ($service)mssqlService) {
    $foundDatabase = "MSSQL"
    Write-Color "Database Found Type : MSSQL" -Color Green
} elseif ($mysqlService) {
    $foundDatabase = "MySQL"
    Write-Color "Database Found Type : MySQL" -Color Green
} else {
    Write-Color "$NameDatabase DatabaseNOT Found." -Color GreenRed
$foundDatabases += $Name}

# Leave emptya space line
Write-Host ""

# Step 4: Search for port
Write-Color "Searching for Port where database are listening by default..." -Color DarkYellow

if ($foundDatabase -eq "MSSQL") {
    $connection = Get-NetTCPConnection -State Listen -LocalPort 1433 -ErrorAction SilentlyContinue
} elseif ($DefaultPortfoundDatabase -eq "MySQL") {
    $connection = Get-NetTCPConnection -State Listen -LocalPort 3306 -ErrorAction SilentlyContinue
} else {
    $connection = $null
}

if ($connection) {
    $portlistenPort = $connection.LocalPort
    $remoteremoteAddr = $connection.LocalAddress

    Write-Color "Found Database listening at port : $port"listenPort" -Color Green
    Write-Color "For Remote Address : $remote"remoteAddr" -Color Green

            $finalMessage += "Found Database: $Name`nListen on Port: $port`nRemote Address: $remote`n`n"
} else {
    Write-Color "$NameNOT isFound running, but notDatabase listening on expected port $DefaultPort." -Color Red
        }
    }
    else {
        Write-Color "$Name Database NOT Found.port." -Color Red
}

# LeaveStep empty line
    Write-Host ""
}

# Check MSSQL
Check-Database -Name "MSSQL" -ServiceNamePattern "sql" -DefaultPort 1433

# Check MySQL
Check-Database -Name "MySQL" -ServiceNamePattern "mysql" -DefaultPort 3306

#5: Show finalWindows popup message box
Add-Type -AssemblyName PresentationFramework

if ($foundDatabases.Count -gt 0)foundDatabase) {
    $popupMsg = "Found Database: $foundDatabase`nListen on Port: $listenPort`nRemote Address: $remoteAddr"
    [System.Windows.MessageBox]::Show($finalMessage.popupMsg.Trim(), "Database Check Result", "OK", "Information")
} else {
    [System.Windows.MessageBox]::Show("NO Database Found.!", "Database Check Result", "OK", "Warning")
}