Skip to content

Error when multiple use of Invoke-SqlCmd2 against the same SQLConnection with -Verbose #6

@jeffchulg

Description

@jeffchulg

Hi,

I've experienced an issue using Invoke-SQLCmd2 against the same instance of a SQLConnection object.

The command is :
Invoke-SQLCmd2 -SQLConnection $sqlconn -Query "PRINT 'Hello';" -Verbose

The first time, I get the expected output, which is VERBOSE: Hello
But, each time I run the command, I get extra lines that come.

The problem is there (there is a little modification from @RamblingCookieMonster's repository, but that do not change the things here)

Following EventHandler is used for PRINT and RAISERROR T-SQL statements. Executed when -Verbose parameter specified by caller

       if ($PSBoundParameters.Verbose)
       {
           $conn.FireInfoMessageEventOnUserErrors=$false # Shiyang, $true will change the SQL exception to information
           $handler = [System.Data.SqlClient.SqlInfoMessageEventHandler] { Write-Verbose "$($_)" }
           $conn.add_InfoMessage($handler)
       }

Is there a way to detect a SQL Connection has already a "verbose handler" ?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions