How-To (Certificate Manager-Self-Hosted): Test SQL connection using PowerShell

How-To (Certificate Manager-Self-Hosted): Test SQL connection using PowerShell

145
Created On 11/12/26 15:36 PM - Last Modified 11/12/26 16:28 PM


Symptom


Certificate Manager-Self-Hosted can show database related error messages when there is a problem connecting to the database. We can utilize PowerShell to verify SQL connectivity outside of our application to verify if there is a database issue.

 

This example script will make a connection to the database, and run a simple SQL query to show you the results in a SQL table. The data in the result isn't really important, but that data is returned. The SQL statement being ran can be found on line #30:

$query = "SELECT TOP 10 * FROM db_schema_version"



Environment


TPP/TLSPDC TLSPDC

Certificate Manager-Self-Hosted



Resolution


  1. Remote into the target application server.
  2. Open PowerShell ISE as administrator or the TPF service account.
  3. Copy and paste the following code into your ISE window:
    1. # Define your SQL Server connection parameters
      $serverName = "Your_SQL_Server_IP_Or_Host"
      $port = "1433"
      $databaseName = "Name_Of_Your_Venafi_DB"
      
      # Ask the user whether to use Integrated Authentication
      $useIntegratedAuth = Read-Host "Use Integrated Authentication? (will use your current user) (Y/N)"
      if ($useIntegratedAuth -eq "Y" -or $useIntegratedAuth -eq "y") {
      $connectionString = "Server=$serverName,$port;Database=$databaseName;Integrated Security=True;"
      } else {
      # Prompt for username and password
      $username = Read-Host "Enter Local SQL Username"
      $password = Read-Host -AsSecureString "Enter SQL Password"
      
      # Convert the secure password to plain text
      $plainPassword = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto([System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($password))
      
      # Build the SQL Server connection string with username and password
      $connectionString = "Server=$serverName,$port;Database=$databaseName;User ID=$username;Password=$plainPassword;"
      }
      
      # Create a .NET SqlConnection object
      $connection = New-Object System.Data.SqlClient.SqlConnection
      $connection.ConnectionString = $connectionString
      
      # Open the connection
      $connection.Open()
      
      # Define your SQL query
      $query = "SELECT TOP 10 * FROM db_schema_version"
      
      # Create a .NET SqlCommand object
      $command = $connection.CreateCommand()
      $command.CommandText = $query
      
      # Execute the query and retrieve the results
      $reader = $command.ExecuteReader()
      
      # Loop through the results and display all data
      while ($reader.Read()) {
      # Loop through the columns and display each value
      for ($i = 0; $i -lt $reader.FieldCount; $i++) {
      $columnName = $reader.GetName($i)
      $columnValue = $reader.GetValue($i)
      Write-Host "$columnName $columnValue"
      }
      Write-Host "------------------------" # Separator between rows
      }
      
      # Close the data reader and the connection
      $reader.Close()
      $connection.Close()
  4. Update the variable at line #2 with your SQL server hostname or IP.
  5. Update the variable at line #4 with your database name.
  6. Run the script and follow the prompts.
  7. The script will prompt if you'd like to use Integrated Authentication, if you select "Y" for yes, then it will attempt to authenticate to the DB using the user you are running PowerShell as. If you select "N" for no, then you will be asked to provide a username and password (This username and password can't be a user from Active Directory, it needs to be a local SQL user).
  8. Process complete.


Actions
  • Print
  • Copy Link

    https://knowledgebase.paloaltonetworks.com/KCSArticleDetail?id=kA1hj0000001kX3CAI&lang=en_US&refURL=http%3A%2F%2Fknowledgebase.paloaltonetworks.com%2FKCSArticleDetail