1 0

Allow SQL Server in Windows Firewall

New-NetFirewallRule -DisplayName "SQLServer default instance" -Direction Inbound -LocalPort 1433 -Protocol TCP -Action Allow
New-NetFirewallRule -DisplayName "SQLServer Browser service" -Direction Inbound -LocalPort 1434 -Protocol UDP -Action Allow
New-NetFirewallRule -DisplayName "SQLServer sqlservr.exe" -Direction Inbound -Program "D:\Microsoft SQL Server\MSSQL15.GILDE\MSSQL\Binn\sqlservr.exe" -Action Allow

Enable-NetFirewallRule -DisplayName "Windows Management Instrumentation (DCOM-In)"
Enable-NetFirewallRule -DisplayName "Windows Management Instrumentation (WMI-In)"

Start: SQL Server Configuration Manager
Expand: SQL Server Network Configuration
Click: Protocols for SQLINSTANCE
Named Pipes: Enabled TCP/IP: Enabled

Test SQL connectivity

$serverInstance = "SQL\DATABASE"
$username = "sa"
$password = "Passw0rd"
$connectionString = "Server=$serverInstance;User Id=$username;Password=$password;TrustServerCertificate=True;Connection Timeout=5"
try {
    $connection = New-Object System.Data.SqlClient.SqlConnection($connectionString)
    $connection.Open()
    Write-Host "Connection successful!" -ForegroundColor Green
    Write-Host "Server Version: $($connection.ServerVersion)"
    $connection.Close()
} catch {
    Write-Host "Connection failed: $($_.Exception.Message)" -ForegroundColor Red
}