Quantcast
Channel: Windows PowerShell forum
Viewing all articles
Browse latest Browse all 21975

Why does my powershell script only connect to the default SQL instance?

$
0
0

Hi!

I have a Server with 2 SQL instances on it. 1 is a deafult instance and the other is a named instance:

Servername       SPING

Default instance     SPING\MSSQLSERVER

Names Instance     SPING\PARIS

When I run an invoke SQL command it always connects to the default instance, no matter what I specify. Does anyone know how to tell it to connect to the SPING\PARIS instance? Here is my code (I have tried it both with and without port numbers)

$instancename = "SPING\PARIS"
$dbname = "testDB"


Invoke-Sqlcmd -Query "
declare @cmd nvarchar(2000)
declare @out_cmd nvarchar(2000)
set @cmd = 'set nocount on select @@servername'
set @out_cmd = 'sqlcmd -Q ""' + @cmd + '"" >> c:\DecomOutput1.txt'
EXECUTE master.dbo.xp_cmdshell @out_cmd" -ServerInstance $instancename -Database $dbname

When I run this it creates a file called DecomOutput1.txt and the file contains the default instance name SPING

Anyone know what im doing wrong?

Thanks,

Zoe


Viewing all articles
Browse latest Browse all 21975

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>