Powershell Try Catch invoke-sqlcmd

前端 未结 3 1375
耶瑟儿~
耶瑟儿~ 2020-12-17 21:57

I am having problems catching an error in PowerShell when a connection fails to a SQL Server using Invoke-Sqlcmd. This is some generic code to demonstrate the issue:

<
相关标签:
3条回答
  • 2020-12-17 22:18

    Posting additional info to supplement the answer by @KeithHill as an answer since it is too long for a comment.

    If the error record was created by the Write-Error commandlet it is non-terminating and subject to the behavior specified by the -ErrorAction argument or the $ErrorActionPreference system variable. Errors using throw are terminating. See the documentation for Write-Error (Note that PowerShell 4.0 and below do not have the -Exception parameter mentioned in the web page.) and about_Throw in the PowerShell help system.

    If you want to add custom error information and make it a terminating error, throw from your catch block as follows:

    catch
    {
        throw (New-Object System.Exception "Error connecting to server $($server).", $_.Exception)
    }
    

    You can use Write-Error if you want termination to behave as specified by the -ErrorAction argument. In PowerShell 4.0 and below the Write-Error commandlet does not allow a -Exception argument and therefore will not provide an InnerException. That means that the caller would have to examine the collection in the $Error system variable if it needs to determine the original exception. In later versions you can Use Write-Error -Message "Some additional error information." -Exception $_.Exception in your catch block.

    0 讨论(0)
  • 2020-12-17 22:20

    Try

    CLS
    $server = "Localhost/fake"
    try
    {
        Invoke-Sqlcmd -Query "SELECT DB_NAME() as [Database]" -Server $server
    }
    catch
    {
        $_ | Out-Null
        Write-Host "Error connecting to server " $server
    }
    

    This will capture the error and redirect it to null and display your write-host

    0 讨论(0)
  • 2020-12-17 22:31

    It would appear that error is considered non-terminating which is a bit odd. Try the Invoke-SqlCommand with an additional parameter: -ErrorAction Stop. If there error is non-terminating, this will convert it to a terminating error that you can catch.

    0 讨论(0)
提交回复
热议问题