Maarten de Vries
Maarten de Vries

Reputation: 23

Powershell Invoke-SqlCmd: Specify port

I want to connect to a database to execute a script on that database, however, I can only access the database in question with a specific port number.

I have something along these lines in terms of code:

Invoke-SqlCmd -ServerInstance "Servername,53806" -Database "DatabaseName" -InputFile $File.Fullname -Verbose | Out-File -filePath "D:\Test\TestSQLCmd.rpt"

When I run this PS snippet, nothing happens at all. Nothing is written into the report. Nothing is written to the console either. When I change the snippet around to:

Invoke-SqlCmd -ServerInstance "Servername","53806" -Database "DatabaseName" -InputFile $File.Fullname -Verbose | Out-File -filePath "D:\Test\TestSQLCmd.rpt"

or

Invoke-SqlCmd -ServerInstance Servername,53806 -Database "DatabaseName" -InputFile $File.Fullname -Verbose | Out-File -filePath "D:\Test\TestSQLCmd.rpt"

I get errors saying that Value cannot be null for the parameter ServerInstance.

If I remove the port, the scripts takes a bit longer to run, resulting in the obvious 'a network related or instance specific error occurred while establishing a connection' error notification.

How should I specifiy a port when using Invoke-SqlCmd in the ServerInstance parameter.

Upvotes: 2

Views: 15769

Answers (2)

kekimian
kekimian

Reputation: 947

Try this:

Invoke-SqlCmd -ServerInstance `"Servername,53806`" -Database "DatabaseName" -InputFile $File.Fullname -Verbose | Out-File -filePath "D:\Test\TestSQLCmd.rpt"

Upvotes: -1

Rachel Ambler
Rachel Ambler

Reputation: 1604

Your first syntax is correct, e.g. the following works for me:

Invoke-SqlCmd -ServerInstance "DbServer,29487" -Database "Master" -Verbose -Query "Select @@ServerName"

However make sure you have configured TCP connectivity. If the named instance is working it may be you're going across a named pipes connection.

An easy way to test would be to use Telnet:

telnet server port

And see if it connects. If it doesn't then that's where you need to focus your attention.

(Telnet may need to be installed as a feature if it's not found).

Upvotes: 4

Related Questions