Windows Vista Forums

Powershell EventHandling syntax with SQL Server?
  1. #1


    christhorsen Guest

    Powershell EventHandling syntax with SQL Server?

    How do I add a script block as an event handler?

    In C# I can use the following:

    SqlConnection conn = new SqlConnection("Data Source=.;Initial
    Catalog=master;INtegrated Security=True");
    conn.FireInfoMessageEventOnUserErrors = true;
    conn.InfoMessage += new SqlInfoMessageEventHandler(conn_InfoMessage);

    When I tried to run this version:

    $SQLConnection = new-object System.Data.SqlClient.SqlConnection("Data
    Source=.;Integrated Security=SSPI;Initial Catalog=master");
    $SQLConnection.Open()
    $handler = [System.Data.SqlClient.SqlInfoMessageEventHandler] {Write-
    Host "Hello World"; Write-Host "$($_)"}
    $SQLConnection += $handler

    I get the error:

    Method invocation failed because
    [System.Management.Automation.PSObject] doesn't contain a method named
    'op_Addition'.
    At H:\a\sqlevent.ps1:10 char:18
    + $SQLConnection += <<<< $handler

    Any help or links on how to do this would be vastly appreciated!

    Thanks,

    --Chris



      My System SpecsSystem Spec

  2. #2


    Marco Shaw [MVP] Guest

    Re: Powershell EventHandling syntax with SQL Server?

    christhorsen@xxxxxx wrote:

    > How do I add a script block as an event handler?
    >
    > In C# I can use the following:
    >
    > SqlConnection conn = new SqlConnection("Data Source=.;Initial
    > Catalog=master;INtegrated Security=True");
    > conn.FireInfoMessageEventOnUserErrors = true;
    > conn.InfoMessage += new SqlInfoMessageEventHandler(conn_InfoMessage);
    >
    > When I tried to run this version:
    >
    > $SQLConnection = new-object System.Data.SqlClient.SqlConnection("Data
    > Source=.;Integrated Security=SSPI;Initial Catalog=master");
    > $SQLConnection.Open()
    > $handler = [System.Data.SqlClient.SqlInfoMessageEventHandler] {Write-
    > Host "Hello World"; Write-Host "$($_)"}
    > $SQLConnection += $handler
    Give this a try:

    $SQLConnection.add_InfoMessage($handler)

    I just used get-member to try to figure out the syntax, but wasn't able
    to actually test it out.

    Marco

    --
    Microsoft MVP - Windows PowerShell
    http://www.microsoft.com/mvp

    PowerGadgets MVP
    http://www.powergadgets.com/mvp

    Blog:
    http://marcoshaw.blogspot.com

      My System SpecsSystem Spec

  3. #3


    christhorsen Guest

    Re: Powershell EventHandling syntax with SQL Server?

    Marco,

    That worked well. The behavior is a bit different than in C# because
    the Powershell output from multiple raiserror (with no_wait)
    statements all arrives in one call to the handler

    In C# the function would be called once per raiserror.

    It's still way better than what I had.

    Thanks,

    --Chris

      My System SpecsSystem Spec

Powershell EventHandling syntax with SQL Server? problems?

Similar Threads
Thread Thread Starter Forum Replies Last Post
VBScripter trying to understand PowerShell syntax Jan PowerShell 5 07 Jul 2008
Help with Powershell syntax DaveKingston PowerShell 26 25 Oct 2007
Powershell syntax highlighting for SciTE? Robin Moffatt PowerShell 1 19 Jul 2007
Powershell.exe command-line syntax EnigmaticSoul PowerShell 4 29 Mar 2007
PowerShell syntax file (ver 0.91) for TextPad Alex K. Angelopoulos [MVP] PowerShell 9 23 Nov 2006