简体   繁体   中英

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.

Your first syntax is correct, eg 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).

尝试这个:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM