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:
Try
CLS $server = "Localhost/fake" try { Invoke-Sqlcmd -Query "SELECT DB_NAME() as [Database]" -Server $server } catch { $_ | Out-Null Write-Host "Error connecting to server " $server }
This will capture the error and redirect it to null and display your write-host