Skip to content

Find-DbaInstance - Fix TcpConnected false for default instances#10327

Merged
potatoqualitee merged 3 commits intodevelopmentfrom
claude/issue-10322-20260405-1718
Apr 7, 2026
Merged

Find-DbaInstance - Fix TcpConnected false for default instances#10327
potatoqualitee merged 3 commits intodevelopmentfrom
claude/issue-10322-20260405-1718

Conversation

@andreasjordan
Copy link
Copy Markdown
Collaborator

When Browser scan type is used, default SQL Server instances do not report a TCPPort via Browser UDP (clients are expected to use 1433). On SQL Server 2022+ where the Browser service is deprecated, no ports are returned at all.

Both cases caused TcpConnected to remain false even when the instance was reachable over TCP.

Fix:

  1. After Browser scan, fall back to testing default TCP port(s) if Browser returned no port info.
  2. When BrowseReply exists but has no TCPPort (default instance), set TcpConnected/Port from any open port found in PortsScanned.

Fixes #10322

Generated with Claude Code

When Browser scan type is used, default SQL Server instances do not
report a TCPPort via Browser UDP (clients are expected to use 1433).
On SQL Server 2022+ where the Browser service is deprecated, no ports
are returned at all.

Both cases caused TcpConnected to remain false even when the instance
was reachable over TCP.

Fix:
1. After Browser scan, fall back to testing default TCP port(s) if
   Browser returned no port info (covers deprecated/absent Browser).
2. When BrowseReply exists but has no TCPPort (default instance),
   set TcpConnected/Port from any open port found in PortsScanned.

Fixes #10322

Co-authored-by: Andreas Jordan <andreasjordan@users.noreply.github.com>
@andreasjordan
Copy link
Copy Markdown
Collaborator Author

@claude the test still fails in my lab.

PS C:\Users\Admin.ordix> Find-DbaInstance -ComputerName sql03 -ScanType Browser, SqlConnect | Format-Table -Property SqlInstance, SqlConnected, TcpConnected -Verbose

SqlInstance   SqlConnected TcpConnected
-----------   ------------ ------------
sql03\SQL2025         True         True
sql03\SQL2022         True        False
sql03\SQL2019         True        False

The first instance is correct, but the two others are wrong. Maybe this helps?

Maybe you can add some verbose messages that help?

@claude
Copy link
Copy Markdown

claude bot commented Apr 5, 2026

Claude finished @andreasjordan's task in 7m 57s —— View job


Fix pushed

Root cause: Test-TcpPort created a single TcpClient in begin{} and reused it for all ports. After the first successful Connect()+Close(), the client was disposed. Every subsequent port test threw ObjectDisposedException (caught silently → IsOpen=False). This is why only the first port tested (sql03\SQL2025's port) showed TcpConnected=True and the remaining ones showed False.

Changes made (commit ea0aaeb):

  1. Test-TcpPort fix — Create a new TcpClient per port test (moved from begin{} to inside the foreach), with Dispose() in a finally block.

  2. Filter port 0DbaBrowserReply.TCPPort defaults to 0 for instances that don't report a TCP port. Port 0 is now filtered before piping to Test-TcpPort (it was being tested and could leave the socket in a bad state).

  3. Verbose messages — Added throughout the Browser scan path. Run with -Verbose to see:

    • What Browser returned (instance names + ports)
    • Port test results
    • Per-instance: which port was found in BrowseReply, what PortsScanned shows, what TcpConnected was set to

To test with verbose output:

Find-DbaInstance -ComputerName sql03 -ScanType Browser, SqlConnect -Verbose | Format-Table SqlInstance, SqlConnected, TcpConnected

|

github-actions bot and others added 2 commits April 5, 2026 17:36
…s; add verbose messages

The TcpClient in Test-TcpPort was created once (in begin{}) and shared
across all port tests. After the first successful Connect()+Close(), the
client was disposed, causing all subsequent port tests to throw
ObjectDisposedException (caught, returning IsOpen=False).

Fix: create a new TcpClient per port test (moved to process{}) with
Dispose() in a finally block.

Also filter out port 0 from Browser results - DbaBrowserReply.TCPPort
defaults to 0 for instances that don't report a TCP port, and testing
port 0 can leave the socket in an error state.

Added Verbose messages throughout the Browser scan path to aid
debugging: Browser results, port test results, per-instance BrowseReply
TCPPort lookup and outcome.

Co-authored-by: Andreas Jordan <andreasjordan@users.noreply.github.com>
@andreasjordan
Copy link
Copy Markdown
Collaborator Author

Now tests pass in my lab:

image

@andreasjordan andreasjordan marked this pull request as ready for review April 5, 2026 18:04
@potatoqualitee potatoqualitee merged commit eddfeee into development Apr 7, 2026
14 checks passed
@potatoqualitee potatoqualitee deleted the claude/issue-10322-20260405-1718 branch April 7, 2026 18:48
@potatoqualitee
Copy link
Copy Markdown
Member

awesome, thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Find-DbaInstance - TcpConnected returned as false instead of true

2 participants