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:
Anything that goes through the try/catch seems to go through twice and ask for the SQL user/pw twice.
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
}
}