dbatools icon indicating copy to clipboard operation
dbatools copied to clipboard

Test-DBAConnectivity - Check connection to database

Open OwainEsau opened this issue 8 years ago • 3 comments

Just wondering if it would be worthwhile giving this function the ability to check connection to a specific database. I.E.

Test-DBAConnection "localhost" -database "db1"

Then just another member in the output DBConnectSuccess

At the moment i do this manually by running Invoke-SQLCmd2 against the database and catching the error, would be nice to have this all rolled into one.

OwainEsau avatar Nov 26 '18 04:11 OwainEsau

@OwainEsau would it suffice to have the DatabaseStatus value in the output if -database is specified? Or would you rather just have a binary "available/not-available"?

joeywas avatar Dec 09 '18 05:12 joeywas

We are not so sure if this is really needed and how the feature should be exactly working.

So I will move the code from my pull request here for the start of a discussion.

    .PARAMETER Database
        The database(s) to test. If unspecified, only the connection to the instance will be testet.
        [string[]]$Database,
            $output = [PSCustomObject]@{
                ComputerName         = $resolved.ComputerName
                InstanceName         = $instanceName
                SqlInstance          = $instance.FullSmoName
                SqlVersion           = $server.Version
                ConnectingAsUser     = $username
                ConnectSuccess       = $connectSuccess
                AuthType             = $authType
                AuthScheme           = $authscheme
                TcpPort              = $tcpport
                IPAddress            = $resolved.IPAddress
                NetBiosName          = $resolved.FullComputerName
                IsPingable           = $pingable
                PSRemotingAccessible = $remoting
                DomainName           = $resolved.Domain
                LocalWindows         = $localInfo.Windows
                LocalPowerShell      = $localInfo.PowerShell
                LocalCLR             = $localInfo.CLR
                LocalSMOVersion      = $localInfo.SMO
                LocalDomainUser      = $localInfo.DomainUser
                LocalRunAsAdmin      = $localInfo.RunAsAdmin
                LocalEdition         = $localInfo.Edition
            }

            if ($Database) {
                $server.Databases.Refresh()
                foreach ($db in $Database) {
                    Add-Member -InputObject $output -NotePropertyName DatabaseName -NotePropertyValue $db -Force
                    Add-Member -InputObject $output -NotePropertyName DatabaseIsAccessible -NotePropertyValue $server.Databases[$db].IsAccessible -Force
                    Add-Member -InputObject $output -NotePropertyName DatabaseStatus -NotePropertyValue $server.Databases[$db].Status -Force
                    $output
                }
            } else {
                $output
            }

andreasjordan avatar May 24 '21 13:05 andreasjordan

I've considered this type of command a number of times and always think it's best to just use Get-DbaDatabase -Database and see if it's null or presents problems. I agree with my past self again today and will suggest that in lieu of a new command 😅

potatoqualitee avatar Jul 30 '21 13:07 potatoqualitee