I have a database that I connect to using a named pipe. I can connect just fine using sqlcmd from a command window, but I get errors when I try to use powershell and Invoke-Sqlcmd.
The 2 commands below run fine in a cmd window.
1. sqlcmd -S MyServer\MyInstance -d master -Q "select sysdatetime()" 2. sqlcmd -S np:\\MyServer\pipe\MSSQL$MyInstance\sql\query -d master -Q "select sysdatetime()"
Command 1 will work in a PS window. command 2 fails in a PS window with the message
Sqlcmd: Error: Microsoft SQL Server Native Client 11.0 : Named Pipes Provider: Could not open a connection to SQL Server [2]. .
Sqlcmd: Error: Microsoft SQL Server Native Client 11.0 : Login timeout expired.
Sqlcmd: Error: Microsoft SQL Server Native Client 11.0 : A network-related or instance-specific error has occurred while establishing a connection to SQL Server. Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured
to allow remote connections. For more information see SQL Server Books Online..
I attempted to use Invoke-Sqlcmd as well using three variations on the -ServerInstance parameter.
1. Invoke-Sqlcmd -ServerInstance MyServer\MyInstance -database master -Query "select sysdatetime()" 2. Invoke-Sqlcmd -ServerInstance np:\\MyServer\pipe\MSSQL$MyInstance\sql\query -database master -Query "select sysdatetime()" 3. Invoke-Sqlcmd -ServerInstance "np:\\MyServer\pipe\MSSQL$MyInstance\sql\query" -database master -Query "select sysdatetime()"
Command 1 works. Commands 2 and 3 fail with this error.
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) At line:1 char:1 + Invoke-Sqlcmd -ServerInstance np:\\MyServer\pipe\MSSQL$MyInstance\sql\query -d ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [Invoke-Sqlcmd], SqlException + FullyQualifiedErrorId : SqlExectionError,Microsoft.SqlServer.Management.PowerShell.GetScriptCommand Invoke-Sqlcmd : At line:1 char:1 + Invoke-Sqlcmd -ServerInstance np:\\MyServer\pipe\MSSQL$MyInstance\sql\query -d ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ParserError: (:) [Invoke-Sqlcmd], ParserException + FullyQualifiedErrorId : ExecutionFailureException,Microsoft.SqlServer.Management.PowerShell.GetScriptCommand
Thanks for any suggestions and guidance!
Matthew
- Edited by MattIndy Friday, July 17, 2015 7:12 PM