7

I want to create a table using .sql file through PowerShell.

Running following in Windows PowerShell ISE as Administrator:

Invoke-Sqlcmd -InputFile 'D:\SQL Scripts\test.sql' -ServerInstance 'localhost\MSSQLSERVER' -Database 'Test'

But this is giving following 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: SQL Network Interfaces, error: 25 - Connection string is not valid)

Could any one please guide to fix this.

I have already refereed execute .sql using powershell

Community
  • 1
  • 1
Jain Prince
  • 393
  • 1
  • 6
  • 19

1 Answers1

8

I myself got the answer after I manually tried to connect the SQL Server Management Studio.

There it's taking server name as localhost. No need to mention the instance name, it's connecting to default instance.

This worked:

Invoke-Sqlcmd -InputFile 'D:\SQL Scripts\test.sql' -ServerInstance 'localhost' -Database 'Test'
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
Jain Prince
  • 393
  • 1
  • 6
  • 19
  • it is worth mentioning that -ServerInstance can be set to another server and/or a non default named instance: / – developer Nov 14 '19 at 14:37