xxxxxxxxxx
$SqlDefaultPort = 1433
$RegKeyPath = "HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server"
$RegValueName = "DefaultPort"
# Retrieve the default port number if it has been changed
if (Test-Path $RegKeyPath) {
$RegValue = Get-ItemProperty -Path $RegKeyPath -Name $RegValueName -ErrorAction SilentlyContinue
if ($RegValue) {
$SqlDefaultPort = $RegValue.$RegValueName
}
}
Write-Output "The default port number for SQL Server is: $SqlDefaultPort"
xxxxxxxxxx
import pyodbc
server = 'your_server_name'
database = 'your_database_name'
username = 'your_username'
password = 'your_password'
# Create a connection string with the appropriate details
conn_str = f'DRIVER={{SQL Server}};SERVER={server};DATABASE={database};UID={username};PWD={password};PORT=1433'
# Establish a connection to the SQL Server
conn = pyodbc.connect(conn_str)
# Perform SQL operations or queries using the connection object 'conn'
# Close the connection
conn.close()
xxxxxxxxxx
# Import the SQL Server module
Import-Module SQLPS -DisableNameChecking
# Get the default instance and its port number
$instance = 'MSSQLSERVER'
$port = (Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\Instance Names\SQL').$instance
# Display the default port number
Write-Host "The default port for MSSQL is $port"