0

Help! I'm trying to gather a bunch of information from a number of different SQL servers. I need to first verify if we can connect to the server (uses port 1433). If that doesn't work, I just want to output that the port is not open. If it does, I want to first check with regular Windows Authentication to SQL. If that errors, I want to prompt for the local SQL login and password and try again. It seems like I'm almost there, except:

  1. Anything that goes through the try/catch seems to go through twice and ask for the SQL user/pw twice.

  2. The only output I'm getting for all of these, even the ones I know are working is the "Port 1433 not open on $Computer. Unable to gather SQL details." So it seems like it's giving that output either way.

I'm obviously missing something, and wondering if another set of eyes might see it. Here's the code I'm working with:

            #Test Connection to port 1433
                $PortTest = Test-NetConnection -ComputerName $Computer -Port 1433 | Out-Null
                $SQLRemote = $PortTest.TcpTestSucceeded
 
            #If SQL Port connection is open, gather info.  If not tell us.
                If (($SQLRemote -eq 'True')) {

            #Try SQL connection via Windows Auth. If it fails, try SQL auth. If it fails output failure
                try{
                    #Test SQL connection with Windows Auth
                        $ErrorActionPreference = "Stop"; #Make all errors terminating
                        Invoke-Sqlcmd -ServerInstance $DBInstance -Query "Select @@Version" | Out-Null
                    #If Windows Auth connects, gather SQL info
                       $SQL = Invoke-Sqlcmd -ServerInstance $DBInstance -Query "SELECT @@VERSION"
                       $SQL2 = Invoke-Sqlcmd -ServerInstance $DBInstance -Query "SELECT SERVERPROPERTY('ServerName') AS Instance, SERVERPROPERTY('Edition') AS License,SERVERPROPERTY('ProductVersion') AS SQLVersion, SERVERPROPERTY('ProductLevel') AS ServicePackLevel"
                        $SP = $SQL2.ServicePackLevel

                        "               Service Pack: $SP" | Out-File $Output -Append
                    }

                catch{
                    Write-Host "Windows authorization failed on DB for $Computer" -BackgroundColor DarkCyan
                    $SQLUserlocal = Read-Host "Enter user name for local SQL login on $Computer"
                    $SQLPassword = Read-Host "Enter password for $SQLUserlocal on $Computer"
                    Try{
                        #Try connecting to SQL with SQL Authentication
                            $ErrorActionPreference = "Stop"; #Make all errors terminating
                            Invoke-Sqlcmd -ServerInstance $DBInstance -User $SQLUserlocal -password $SQLPassword -Query "Select @@Version" | Out-Null
                        #If SQL Authetnication works, gather info
                            $SQL = Invoke-Sqlcmd -ServerInstance $DBInstance -Query "SELECT @@VERSION"
                            $SQL2 = Invoke-Sqlcmd -ServerInstance $DBInstance -Query "SELECT SERVERPROPERTY('ServerName') AS Instance, SERVERPROPERTY('Edition') AS License,SERVERPROPERTY('ProductVersion') AS SQLVersion, SERVERPROPERTY('ProductLevel') AS ServicePackLevel"
                            $SP = $SQL2.ServicePackLevel
                            "               Service Pack: $SP" | Out-File $Output -Append
                    }
                    Catch{
                        #Output if DB is not reachable
                        "               SQL DB is not reachable on $Computer." | Out-File $Output -Append
                        "               Gather SQL Information manually or verify DB is set up for remote connections and try again" | Out-File $Output -Append
                    }

                    Finally{
                        #Continue on with script
                            $ErrorActionPreference = "Continue"; #Reset the error action pref to default
                    }
                }
                Finally{
                    #Continue on with script
                        $ErrorActionPreference = "Continue"; #Reset the error action pref to default
                    }
            
    }
   
    else{
        "               Port 1433 not open on $Computer. Unable to gather SQL details" | Out-File $Output -Append
        }
            }
Christopher Cass
  • 817
  • 4
  • 19
  • 31
  • Put line `$ErrorActionPreference = "Stop"` **above** the first try, not inside of it – Theo Feb 21 '21 at 15:49
  • I changed it to ``` $ErrorActionPreference = "Stop"; #Make all errors terminating #Test SQL connection with Windows Auth Invoke-Sqlcmd -ServerInstance $DBInstance -Query "Select @@Version" | Out-Null ``` and see no change – Christopher Cass Feb 21 '21 at 15:58
  • No, i meant you put it above the first `try{..` statement, best put it above all code as the very first line – Theo Feb 21 '21 at 16:18
  • Add `-ErrorAction Ignore` and, `throw` to your try block. – Abraham Zinala Feb 21 '21 at 19:00
  • In the first `catch {..}` you ask for a username and password, but never use these. In fact, you are repeating the **exact** same code in the nested `try {..}` block as in the first which failed, so it will fail again. – Theo Feb 22 '21 at 13:53

0 Answers0