我遇到問題,當連接失敗時使用Invoke-Sqlcmd在SQL Server上捕獲PowerShell中的錯誤。這是一些通用的代碼來說明這個問題:Powershell嘗試捕獲invoke-sqlcmd
CLS
$server = "Localhost\fake"
try
{
Invoke-Sqlcmd -Query "SELECT DB_NAME() as [Database]" -Server $server
}
catch
{
Write-Host "Error connecting to server " $server
}
我收到以下錯誤:
Invoke-Sqlcmd : A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)
我期待得到一個行語句:「錯誤連接到服務器localhost \假冒」
我在PowerShell v3上,你的代碼給我你的自定義錯誤。 –
也在powershell v2 –
謝謝,多數民衆贊成在知道,我顯然仍然在2. –