Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8734859
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:55:45+00:00 2026-06-13T09:55:45+00:00

I am deliberately trying to log in to a SQL Server where I do

  • 0

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;
} ; 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T09:55:46+00:00Added an answer on June 13, 2026 at 9:55 am

    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#

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a SQL INSERT statement (deliberately not using ActiveRecord), to eventually
in my unit test I deliberately trying to raise an OutOfMemoryError exception. I use
I'm trying to test out Spring Annotations to see how they work with some
I'm trying to create some 'friend assemblies' using the [InternalsVisibleTo()] attribute, but I can't
I'm trying to do a fuzzy match on the Phrase Grand Prarie (deliberately misspelled)
What is the best way to deliberately delay a response from the JBoss server?
I ran gem sources -c so that I have to deliberately specify where I
Does the Silverlight runtime have to be downloaded/installed deliberately, or these days is there
I am using jquery validation 1.9.0. I have used the latest Jquery and on
I'm trying to configure a global error page for my struts 2 project. So

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.