Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrong regex in function Get-SQLInstanceBrowserUDP in Find-DbaInstance command #9071

Closed
uncletimmy3 opened this issue Sep 4, 2023 · 0 comments · Fixed by #9072
Closed

Wrong regex in function Get-SQLInstanceBrowserUDP in Find-DbaInstance command #9071

uncletimmy3 opened this issue Sep 4, 2023 · 0 comments · Fixed by #9072
Labels
bugs life triage required New issue that has not been reviewed by maintainers

Comments

@uncletimmy3
Copy link
Contributor

uncletimmy3 commented Sep 4, 2023

Verified issue does not already exist?

I have searched and found no existing issue

What error did you receive?

If sql server name contains the minus character -, e.g. foo-bar-01, the command:

Find-DbaInstance -ComputerName "foo-bar-01"  -ScanType Browser

returns nothing, because of regex in function Get-SQLInstanceBrowserUDP.

The problem is in this part of regex (ServerName;(\w+);. The \w character class will match any word character [a-zA-Z_0-9] and doesn't contain the minus character - (see about_regular_expressions), but according to windows naming convention dns host names can contain the minus character -. So this regex can't parse such dns host names correctly.

I suggest replacing (ServerName;(\w+); with (ServerName;([a-zA-Z0-9_-]+);

Steps to Reproduce

As an example, I suggest parsing test SQL Browser response string rather than trying to use Find-DbaInstance command:

> "ServerName;FOO-BAR-01;InstanceName;FB01;IsClustered;No;Version;15.0.2000.5;tcp;58860;;" |
     Select-String "(ServerName;(\w+);InstanceName;(\w+);IsClustered;(\w+);Version;(\d+\.\d+\.\d+\.\d+);(tcp;(\d+)){0,1})" -AllMatches |
     Select-Object -ExpandProperty Matches |
     ForEach-Object {
                             $obj = New-Object Dataplat.Dbatools.Discovery.DbaBrowserReply -Property @{
                                 MachineName  = $computer.ComputerName
                                 ComputerName = $_.Groups[2].Value
                                 SqlInstance  = "$($_.Groups[2].Value)\$($_.Groups[3].Value)"
                                 InstanceName = $_.Groups[3].Value
                                 Version      = $_.Groups[5].Value
                                 IsClustered  = "Yes" -eq $_.Groups[4].Value
                             }
                             if ($_.Groups[7].Success) {
                                 $obj.TCPPort = $_.Groups[7].Value
                             }
                             $obj
                         }
>


> "ServerName;FOO-BAR-01;InstanceName;FB01;IsClustered;No;Version;15.0.2000.5;tcp;58860;;" |
     Select-String "(ServerName;([a-zA-Z0-9_-]+);InstanceName;(\w+);IsClustered;(\w+);Version;(\d+\.\d+\.\d+\.\d+);(tcp;(\d+)){0,1})" -AllMatches |
     Select-Object -ExpandProperty Matches |
     ForEach-Object {
                             $obj = New-Object Dataplat.Dbatools.Discovery.DbaBrowserReply -Property @{
                                 MachineName  = $computer.ComputerName
                                 ComputerName = $_.Groups[2].Value
                                 SqlInstance  = "$($_.Groups[2].Value)\$($_.Groups[3].Value)"
                                 InstanceName = $_.Groups[3].Value
                                 Version      = $_.Groups[5].Value
                                 IsClustered  = "Yes" -eq $_.Groups[4].Value
                             }
                             if ($_.Groups[7].Success) {
                                 $obj.TCPPort = $_.Groups[7].Value
                             }
                             $obj
                         }

MachineName  :
ComputerName : FOO-BAR-01
SqlInstance  : FOO-BAR-01\FB01
InstanceName : FB01
TCPPort      : 58860
Version      : 15.0.2000.5
IsClustered  : False

Please confirm that you are running the most recent version of dbatools

2.0.4 version is used

> Get-Module dbatools | Select -ExpandProperty Version

Major  Minor  Build  Revision
-----  -----  -----  --------
2      0      4      -1

Other details or mentions

No response

What PowerShell host was used when producing this error

PowerShell Core (pwsh.exe), Windows PowerShell (powershell.exe)

PowerShell Host Version

Name                           Value
----                           -----
PSVersion                      7.3.6
PSEdition                      Core
GitCommitId                    7.3.6
OS                             Microsoft Windows 10.0.14393
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

SQL Server Edition and Build number

Microsoft SQL Server 2019 (RTM) - 15.0.2000.5 (X64) Sep 24 2019 13:48:23 Copyright (C) 2019 Microsoft Corporation Developer Edition (64-bit) on Windows Server 2022 Standard 10.0 (Build 20348: ) (Hypervisor)

.NET Framework Version

.NET 7.0.9

@uncletimmy3 uncletimmy3 added bugs life triage required New issue that has not been reviewed by maintainers labels Sep 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bugs life triage required New issue that has not been reviewed by maintainers
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant