Powershell Try Catch invoke-sqlcmd
问题 I am having problems catching an error in PowerShell when a connection fails to a SQL Server using Invoke-Sqlcmd. This is some generic code to demonstrate the issue: CLS $server = "Localhost\fake" try { Invoke-Sqlcmd -Query "SELECT DB_NAME() as [Database]" -Server $server } catch { Write-Host "Error connecting to server " $server } I get the following error: Invoke-Sqlcmd : A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not