Connecting the SMO Events

Sep 23, 2007 at 10:05 PM
I'm trying to connect to the SQL Management Objects to output server messages to a file while a create database script runs with the 1.0 release.

Running the following works:
. .\EventHandler.ps1
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.Smo")
$DatabaseServer = new-object ('Microsoft.SqlServer.Management.Smo.Server') "MYSERVER"
$ConnectionContext = $DatabaseServer.ConnectionContext
Get-EventBinding ConnectionContext -IncludeUnboundEvents | Format-Table -Auto

But I hit a road block at:
Add-EventHandler (get-variable ConnectionContext) ServerMessage { Write-Host "Foo"; } 
Add-EventHandler (get-variable ConnectionContext) InfoMessage { Write-Host "Foo"; }

With the following error:
  • Connect-EventListener : Unable to cast object of type 'Microsoft.SqlServer.Management.Common.ServerConnection' to type 'System.Management.Automation.PSObject'.

Thoughts on work arounds?

Thanks,
Colin
Coordinator
Sep 30, 2007 at 7:24 PM
Hi Colin,

I have SQL server SMO at home here, I'll take a closer look at what's happening and report back here!

Cheers,

- Oisin