3

I am deliberately trying to log in to a SQL Server where I do not have a login to test some error handling with PowerShell 2.0 and SMO using SQL Server 2008 R2.

Here's my script:

param ([String]$instanceName);
[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SMO') | out-null;

$conn = new-object Microsoft.SqlServer.Management.Common.ServerConnection ; 
$conn.LoginSecure = $true; 
$conn.ServerInstance = $instanceName ; 
$conn.NonPooledConnection = $true ;  

try {
    $serverInstance = New-Object Microsoft.SqlServer.Management.Smo.Server ($conn) ; 
}

catch { 
    $err = $Error[0].Exception ; 
    write-host "Error caught: "  $err.Message ; 
    continue ; 
} ; 

Write-Output $serverInstance.Version;

the catch block does not get executed. Any ideas? I also tried to trap it using the trap function, but got the same result.

UPDATE 1

I have changed my script to the following and have got it to catch an exception on line

foreach($j in $serverInstance.Databases) {

If I comment out the foreach loop, the line below the foreach loop does not raise an exception, although it should (IMO).

param ([String]$instanceName);

[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SMO') | out-null; # load the SMO assembly
clear

 $ErrorActionPreference = "Stop";
    $conn = new-object Microsoft.SqlServer.Management.Common.ServerConnection ; 
    $conn.LoginSecure = $false; 
    $conn.Login = "sa" ; 
    $conn.Password = "password" ; 
    $conn.ServerInstance = $instanceName ; 
    $conn.NonPooledConnection = $true ;  

try {
    $serverInstance = New-Object Microsoft.SqlServer.Management.Smo.Server ($conn) ; 
    foreach($j in $serverInstance.Databases) { 
        write-host $j.name ; 
    } ; 
    Write-Output $serverInstance.Databases.Count;
}

catch [Microsoft.SqlServer.Management.Common.ConnectionFailureException] {
    $err = $Error[0].Exception ; 
    write-host "Error caught: "  $err.Message ; 
    while ( $err.InnerException )    {
        $err = $err.InnerException;
        Write-Host "Inner exception:" $err.InnerException.Message;
        };      
    return;
} ; 
3
  • 1
    SMO opens a connection when required and nothing in your try block actually connects to the server. Does moving your Write-Output command into the try block cause an error? Commented Oct 25, 2012 at 15:59
  • @Pondlife moving the $serverInstance.Version to the try block does not cause an error. I get the same behaviour. Commented Oct 26, 2012 at 7:49
  • I've added an updated script to my question under heading UPDATE 1. Commented Oct 26, 2012 at 10:55

1 Answer 1

1

I've found a workaround. Microsoft have confirmed this as a bug in SMO and they do not deem it important enough to fix for now. The workaround is detailed here:

https://connect.microsoft.com/SQLServer/feedback/details/636401/smo-is-inconsistent-when-raising-errors-for-login-faliures#

Sign up to request clarification or add additional context in comments.

1 Comment

Microsoft Connect is retired and the link no longer works

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.