Get-DbaNetworkEncryption - Add command to retrieve TLS certificate from SQL Server network#10293
Get-DbaNetworkEncryption - Add command to retrieve TLS certificate from SQL Server network#10293andreasjordan wants to merge 6 commits intodevelopmentfrom
Conversation
|
@claude I have added two files in this branch:
Get-SqlServerTlsCertificate should be called from Get-DbaNetworkEncryption. We want to preserve the content of Get-SqlServerTlsCertificate as much as possible but some modifications are allowed if needed. Please add the code that is needed to get this working. |
|
Claude finished @andreasjordan's task in 3m 14s —— View job Done
What was implementedThe
Port resolution logic:
Also registered |
(do Get-DbaNetworkEncryption) Co-authored-by: Andreas Jordan <andreasjordan@users.noreply.github.com>
|
The command works in my lab as expected. |
|
@claude we still want to support PowerShell version 3. Would it be possible to change the code of private/functions/Get-SqlServerTlsCertificate.ps1 to be compatible with PowerShell version 3? Don't change the code yet, only analyze. |
|
Claude finished @andreasjordan's task in 1m 31s —— View job Analysis: PowerShell v3 Compatibility for
|
| Line | Current | Fix |
|---|---|---|
| 138 | [byte[]]::new(8) |
New-Object byte[] 8 |
| 166 | [byte[]]::new(8 + $Count) |
New-Object byte[] (8 + $Count) |
| 193 | [System.Net.Sockets.UdpClient]::new($ComputerName, 1434) |
New-Object System.Net.Sockets.UdpClient($ComputerName, 1434) |
| 242 | [System.Net.Sockets.TcpClient]::new() |
New-Object System.Net.Sockets.TcpClient |
| 253–256 | [System.IO.Pipes.NamedPipeClientStream]::new(...) |
New-Object System.IO.Pipes.NamedPipeClientStream(...) |
| 284 | [byte[]]::new(8) |
New-Object byte[] 8 |
| 295 | [byte[]]::new($payloadLength) |
New-Object byte[] $payloadLength |
| 340 | [TdsTlsStream]::new($targetStream) |
New-Object TdsTlsStream $targetStream (after Add-Type fix) |
| 348 | [System.Net.Security.SslStream]::new(...) |
New-Object System.Net.Security.SslStream(...) |
| 367 | [System.Security.Cryptography.X509Certificates.X509Certificate2]::new(...) |
New-Object System.Security.Cryptography.X509Certificates.X509Certificate2(...) |
Issue 3 — [Ordered]@{} (lines 204, 218)
$instanceInfo = [Ordered]@{}Ordered hashtables via [Ordered] were introduced in PowerShell v3, so this is actually fine and does not need to change.
Issue 4 — .GetAwaiter().GetResult() (line 248)
$null = $connectTask.GetAwaiter().GetResult()GetAwaiter() is a .NET 4.5 method. PowerShell 3.0 runs on .NET 4.0, where this method does not exist. In practice most systems with PS3 also have .NET 4.5+ installed, but technically this is a risk.
Fix approach: Remove this call. Since .Wait() on line 244 already blocks until the task completes (and throws if it faulted), calling .GetAwaiter().GetResult() afterward is redundant — its only purpose here is to re-throw any exception from the task, but .Wait() already does that. The line can simply be removed.
Summary
| Issue | PS Version Introduced | Fixable? |
|---|---|---|
class keyword |
v5.0 | Yes — Add-Type with C# |
::new() constructors (×10) |
v5.0 | Yes — New-Object |
[Ordered]@{} |
v3.0 | No change needed |
.GetAwaiter().GetResult() |
.NET 4.5 | Yes — remove the line |
All issues are fixable. The most significant change is converting TdsTlsStream to a C# Add-Type block, but the internal logic of the class stays exactly the same — it's purely a syntax translation from PowerShell class syntax to C#.
|
@claude please implement the suggested changes. |
|
Claude finished @andreasjordan's task in 3m 46s —— View job Implementing PowerShell v3 Compatibility Changes
DoneAll four PowerShell v3 incompatibilities have been fixed in 1. 2. All
3. 4. SSL validation callback extracted |
- Replace PowerShell class with Add-Type C# block (classes require PS v5+) - Replace all ::new() constructor calls with New-Object (PS v3 compatible) - Remove .GetAwaiter().GetResult() call (.NET 4.0 compatible; .Wait() already handles the result) - Extract SSL validation callback to named variable for New-Object compatibility (do Get-DbaNetworkEncryption) Co-authored-by: Andreas Jordan <andreasjordan@users.noreply.github.com>
|
I'm very happy with this command and it works in my lab. But I would like to remove the port from the output as we don't get the used port from the private function and the output is all about the certificate and the port is not so importent. What do you think @potatoqualitee ? |
Adds
Get-DbaNetworkEncryptionwhich retrieves the TLS/SSL certificate presented by a SQL Server instance during the TLS handshake, without requiring Windows host access or WinRM.Key features:
This complements
Get-DbaNetworkCertificate(which reads from the Windows registry, requires WinRM) by instead showing what certificate is actually being presented to clients.