Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8245413
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T22:10:13+00:00 2026-06-07T22:10:13+00:00

We are using Invoke-sqlcmd cmdlet to execute SQL query using powershell. If SQl server

  • 0

We are using Invoke-sqlcmd cmdlet to execute SQL query using powershell.

If SQl server instance is given as wrong it is throwing exception.

Though i have captured the error in try catch still it throw exception in console as

“Invoke-Sqlcmd : A network-related or instance-specific error occurred
while establishing a connection to SQL Server”

            try {
            $Qresult= Invoke-sqlcmd -query $SelectQuery -ServerInstance $srvInstance
            $Qresult = $Qresult| % { $_.$columnName+"`n" }
            LogWrite "$Qresult`n"
        }
        catch {
            Write-error "Error occured when executing sql $SelectQuery"
            LogWrite $Error[0]
        }

How to verify whether SQL server instance is available and it is running before executing any query?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T22:10:15+00:00Added an answer on June 7, 2026 at 10:10 pm
    function SQL-Ping-Instance 
    {
        param (
            [parameter(Mandatory = $true)][string] $ServerInstance,
            [parameter(Mandatory = $false)][int] $TimeOut = 1
        )
    
        $PingResult = $false
    
        try
        {
            $SqlCatalog = "master"
            $SqlConnection = New-Object System.Data.SqlClient.SqlConnection
            $SqlConnection.ConnectionString = "Server = $ServerInstance; Database = $SqlCatalog; Integrated Security = True; Connection Timeout=$TimeOut"
            $SqlConnection.Open() 
            $PingResult = $SqlConnection.State -eq "Open"
        }
    
        catch
        {
        }
    
        finally
        {
            $SqlConnection.Close()
        }
    
        return $pingResult
    }
    
    if (SQL-Ping-Instance $srvInstance)
    {
        $Qresult= Invoke-sqlcmd -query $SelectQuery -ServerInstance $srvInstance
        $Qresult = $Qresult| % { $_.$columnName+"`n" }
        LogWrite "$Qresult`n"
    }
    else
    {
        LogWrite "Couldn't contact $srvInstance"
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have recently began using C# to invoke powershell scripts, with some success :)
I have an instance method that I'd like to invoke directly using the callback
I have 2 powershell scripts that I execute from c# which I'm using to
I'm using invoke-expression in PowerShell to create an archive, but it's not working due
I need some help fixing a cross-thread exception. I am using Invoke which usually
I am using invoke-sqlcmd to manage my databases, it works fine. I save the
what snapins or initializations do I need to run a sql server powershell script
I'm writing a PowerShell script that will execute commands on a remote host using
I have an powershell script which is executing sql scripts from a given folder.
I have a SQL 2005 instance that runs a job that uses a Powershell

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.