This discussion has been locked. The information referenced herein may be inaccurate due to age, software updates, or external references.
You can no longer post new replies to this discussion. If you have a similar question you can start a new discussion in this forum.

Remote Db Connectivity Test

I've got an application owner who thinks he is having issues with his app server connecting to his database server. I've offered to create a monitor that would run from his APP server and make a DB connection to his SQL server. I'm attempting to do this with a PS script type component designated to run on the "Remote Host". Using this script.

try {
$connectionString = "Server=MyServer;Database=EFTDB;Integrated Security=False;User ID = ${USER};Password = ${PASSWORD}";
$sqlConnection = New-Object System.Data.SqlClient.SqlConnection $connectionString;
$sqlConnection.Open();
$msg = "True";
Write-Host "Message: $msg";
Write-Host "Statistic: 1";
} catch {
$ErrorMessage = $_.Exception.Message
$FailedItem = $_.Exception.ItemName
$msg = "False $ErrorMessage";
Write-host "Message: $msg";
Write-host "Statistic: 0";
## EXIT 1
} finally {
## Close the connection when we're done
$sqlConnection.Close()
## EXIT 0
}

No matter what I do, I keep getting the error Message: False Exception calling "Open" with "0" argument(s): "Login failed for user 'AD\user'."
Statistic: 0

If I change the execution mode to "Local Host" and the script to Integrated Security, it works

try {
$connectionString = "Server=MyServer;Database=EFTDB;Integrated Security=True";
$sqlConnection = New-Object System.Data.SqlClient.SqlConnection $connectionString;
$sqlConnection.Open();
$msg = "True";
Write-Host "Message: $msg";
Write-Host "Statistic: 1";
} catch {
$ErrorMessage = $_.Exception.Message
$FailedItem = $_.Exception.ItemName
$msg = "False $ErrorMessage";
Write-host "Message: $msg";
Write-host "Statistic: 0";
} finally {
## Close the connection when we're done
$sqlConnection.Close()
}

Again the idea is for the script to run on the app server and create a connection to the Db server from there. I believe "Local Host" mode would cause it to run from the poller?

So what am I doing wrong? I 100% know the account has access to that Db, I am logged into it as that account via MSSMS. The AppInsight for MSSQL works fine with that account. Is there another approach I should be taking?

Parents
  • Does the script work if you run it manually on the application server?

    I use below as a general way of Orion asking another server to check connectivity to a third server. Not a fancy SQL login as you do but it will show if it can connect anyway.

    $Client = '${IP}'
    $ClientPort = "5985"
    $Server = $args[0]
    $ServerPort = $args[1]
    
    # Connect to client.
    try
    {
        $PSSessionOption = New-PSSessionOption -SkipCACheck -SkipCNCheck
        $Session = New-PSSession -SessionOption $PSSessionOption -ComputerName $Client -Port $ClientPort -Credential '${CREDENTIAL}' -ErrorAction Stop
    }catch
    
    {
         Write-host "Statistic.StatusCheck: 2"
         write-host "Message.StatusCheck: Critical! Can't connect to $Client"
    exit
    }
    
    try
    {
         $ScriptBlock =
         {
               param (
                    [parameter(Mandatory = $true)]
                    [string]$Server,
                    [parameter(Mandatory = $true)]
                    [string]$ServerPort
               )
               
                    $Tcptest = (Test-NetConnection -ComputerName $Server -Port $ServerPort).TcpTestSucceeded
                    
                    if ($Tcptest -eq $true)
                    {
                         Write-Host "Statistic.StatusCheck: 0"
                         Write-Host ""
                         Write-Host "Message.StatusCheck: OK! $Server reachable on TCP port $ServerPort"
                    }
                    else
                    {
                        Write-Host "Statistic.StatusCheck: 1"
                         Write-Host ""
                         Write-Host "Message.StatusCheck: Critical! $Server unreachable on TCP port $ServerPort"
                    }
               
         }
         
         
         # Run scriptblock on remote computer.
         Invoke-Command -Session $Session -ScriptBlock $ScriptBlock -ArgumentList $Server,$ServerPort
         
         
         # Disconnect session.
         Remove-PSSession $Session
    }catch
    {
         # Disconnect session.
         Remove-PSSession $Session
    }
    

  • Yes, run from within a Windows PowerShell ISE it returns Success

    try {
    $connectionString = "Server=musagssql01prd.murphyusa.local;Database=EFTDB;Integrated Security=True";
    $sqlConnection = New-Object System.Data.SqlClient.SqlConnection $connectionString;
    $sqlConnection.Open();
    $msg = "True";
    Write-Host "Message: $msg";
    Write-Host "Statistic: 1";
    } catch {
    $ErrorMessage = $_.Exception.Message
    $FailedItem = $_.Exception.ItemName
    $msg = "False $ErrorMessage";
    Write-host "Message: $msg";
    Write-host "Statistic: 0";
    } finally {
    ## Close the connection when we're done
    $sqlConnection.Close()
    }

    Do you run your PS component in "Local Host" execution mode? In other words your script runs on the poller. It makes a PSSession to the "APP" server and then runs a script block that test the connection to the third server. Poller-->AppServer-->DbServer.

  • Yes I do.

    In Script argument i have "IP to test against (dbserver), port to test against"


  • Thank you for the idea. It definitely works to show that from a network point of view connections are possible so that's half the battle and I can use that. I'll see if I can figure out how to make the DB connection as part of the script block.

  • Did you try ODBC user experience monitor? You can specify the connection string and the test will run from the server where the agent is installed. You will probably also need to install some additional ODBC drivers for different DB technologies .

  • I looked at that briefly. I'm 99% agentless. I guess you are saying that the Template/Component would be deployed to the APP server and this would cause the APP server to attempt the ODBC connection to the Db? The ODBC drivers would need to be on the APP server in this case?

  • You install the SolarWinds agent on the APP server. You don't even need to change the full node from WMI/SNMP to agent, jut the application template.

    It is also very useful for when the IT security blocks remote powershell

Reply Children
No Data