PS invoke-sql error catching

Posted on 2013-12-26
Last Modified: 2013-12-30
I'm using invoke-SQL and it is not catching this error and the program cycles through the all of the tables in the database without telling me there was an issue:
"The statement terminated. The maximum recursion 100 has been exhausted before statement completion."

Is there something wrong with this code? I just need the program to stop and tell me if there's an issue.
$ErrorActionPreference = "Stop"


$CSV = Invoke-Sqlcmd -Query $Query -ErrorAction Stop
catch{ [System.Exception]
    $errorout += "major error, query failed problem"
    $errorout += $error[0]

Question by:johnj_01201
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
LVL 70

Expert Comment

ID: 39745330
Can you be more specific about "this error"? Is the main loop executed (and Invoke-SQL not part of it)?

Author Comment

ID: 39745644
The program continued to run as normal, showing no results for most of the tables the recursion error was taking place with. I set the recursion level to 32767 in the SQL query to get rid of the  recursion error, but I thought the try\catch code wrapped around the invoke-sql statement would have caught the error and stopped the program to tell me?
LVL 70

Expert Comment

ID: 39745855
So your query is producing recursion (a CTE?), and issues with that are not reflected as exception/error? That might be an issue with Invoke-Sql. I'll try to debug that. It might be necessary you show us the (simplified) query you run.
LVL 70

Expert Comment

ID: 39745865
BTW, the catch doesn't make much sense. [System.Exception] is wrong here: You are adding a string and an ErrorRecord to $errorout.

After catching the error, you continue with the script part "MAIN PROGRAM LOOP STARTS HERE". Is that what you want to do? Providing some more context might be necessary for us to understand.

It would also be interesting to see how the error message should be constructed and displayed (if so).
LVL 70

Accepted Solution

Qlemo earned 500 total points
ID: 39745876
Tests run successful. If I get recursion error in Invoke-Sql, the exception handler is triggered. This is my test case:
$ErrorActionPreference = "Stop"
remove-variable errorout

$Query = @"
with cte as 
( select 1 as no
  union all
  select * from cte
select * from cte

$CSV = Invoke-Sqlcmd -Query $Query -ErrorAction Stop -ServerInstance mssqlserver -Database MyDB -Username Me -Password Secret
    $errorout += $error[0]
    Write-Error "major error, query failed problem"

Write-Host "... and the story continues ..."

Open in new window

It halts, because of Write-Error, creating another error entry with the custom message.

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
Auditing domain password hashes is a commonly overlooked but critical requirement to ensuring secure passwords practices are followed. Methods exist to extract hashes directly for a live domain however this article describes a process to extract u…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question