Solved

PS invoke-sql error catching

Posted on 2013-12-26
5
672 Views
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.
Code:
[System.GC]::Collect()
$ErrorActionPreference = "Stop"

PROGRAM CODE HERE

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

MAIN PROGRAM LOOP STARTS HERE
0
Comment
Question by:johnj_01201
  • 4
5 Comments
 
LVL 68

Expert Comment

by:Qlemo
Comment Utility
Can you be more specific about "this error"? Is the main loop executed (and Invoke-SQL not part of it)?
0
 
LVL 1

Author Comment

by:johnj_01201
Comment Utility
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?
0
 
LVL 68

Expert Comment

by:Qlemo
Comment Utility
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.
0
 
LVL 68

Expert Comment

by:Qlemo
Comment Utility
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).
0
 
LVL 68

Accepted Solution

by:
Qlemo earned 500 total points
Comment Utility
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
$error.Clear()

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

try
{
$CSV = Invoke-Sqlcmd -Query $Query -ErrorAction Stop -ServerInstance mssqlserver -Database MyDB -Username Me -Password Secret
}
catch{ 
    $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.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Hi all.   The other day I had to change the passwords for a bunch of users on the fly. Because they were so many, I decided to do it in an automated way and I would like to share it with you all.   If you are not doing it directly in a Domain Co…
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

743 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now