Skip to content

Add ability to find LocalDB instances with Find-DbaInstance #8265

Description

@JohnNelsonNumber2

Summarize Functionality

Find-DbaInstance doesn't seem to detect any localdb instances, which to me should be in scope since localdb is basically service-less express edition. It would be great to see this functionality in Find-DbaInstance. (see technical details)

Is there a command that is similiar or close to what you are looking for?

No

Technical Details

One can run sqllocaldb info and get info on localdb instances owned by the current user and all shared instances.

image

and one can also run sqllocaldb info 'instance name' and get more info about them

image

So...here's a very VERY rough attempt at showing all the pieces I think are needed to detect LocalDb instances

# attempt to get the path to SqlLocalDb.exe
$localDbExe = (Get-Command SqlLocalDb.exe -ErrorAction SilentlyContinue).Path;
#if it wasn't immediately found, attempt to get it from the registry
if(-not $localDbExe) {
    $localDbExe = try {
        #attempt to get the location for the sql tools from the registry
        Join-Path (Get-Item (Join-Path (Get-ChildItem 'HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server' |
            #get only number-based subkeys which represent different versions (130,140,150) and where ..\tools\ClientSetup exists
            Where-Object {$_.PSChildName -match '^\d+$' -and (Test-Path (Join-Path $_.PSPath 'tools\ClientSetup'))} |
                #sort by the name and get the last one (should be the latest version), then get the Path value from it and add SqlLocalDb.exe to the end
                Sort-Object -Property Name | Select-Object -Last 1).PSPath '\tools\ClientSetup')).GetValue('Path') 'SqlLocalDb.exe'
    }
    catch {}
}
#if we've got a SqlLocalDb.exe path and it exists...
if(Test-Path $localDbExe -ErrorAction SilentlyContinue) {
    #get localdb instances from the command
    $instances = Invoke-Command {& $localDbExe info};
    #for each localdb instance (owned by or shared with current user)...
    foreach($instance in $instances) {
        $currObject = @{}
        #get all the <name> : <value> pairs from ' SqlLocalDb.exe info <instance> '
        Invoke-Command {& $localDbExe info $instance} | ForEach-Object {
            $parts = $_.Split(':',2);
            if($parts.Count -eq 2) {
                $currObject."$($parts[0].Trim())" = "$($parts[1].Trim())"
            }
        }
        #and output them        
        [PSCustomObject]$currObject;
    }
}

And here's the output

image

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions