5

I am using latest version of Powershell core on Linux Ubuntu 16.04. I am trying to query a sql server database using powershell's invoke-sqlcmd cmdlet. (Yes I have installed the sqlserver module) I get an error which says that it cannot find the cmdlet. The command is there in windows powershell when I install the sqlserver module. Screenshot of the same

Does invoke-sqlcmd only work on Windows? If yes,is there another way I can achieve the same result in powershell core.

5
  • Found the solution. Instead of using the Invoke-sqlcmd, I am using the plain sqlcmd inside the invoke-command. Thats working well. Commented Sep 7, 2018 at 9:58
  • would you elaborate on how you did that? Commented Nov 2, 2018 at 2:51
  • Since "sqlcmd" will still work in the server hosting the database. I went for the invoke-command cmdlet. inside the invoke-command I invoked the invoke-expression command and I passed the sqlcmd as a parameter to the invoke-expression cmdlet. Commented Nov 11, 2018 at 12:30
  • 1
    What like Invoke-Command Invoke-Expression "sqlcmd -i ./dropDatabaseScript.sql"? Maybe you can put that as an answer? with more details? Commented Nov 13, 2018 at 9:15
  • Indeed above worked for me as well! I split it in four parts. I had a folder with multiple scripts i wanted to run $files = "./*.sql"; $command = 'sqlcmd -i $_';(also added dbname/user/psw and such) $expression = { Invoke-Expression $command }; Resolve-Path $files | ForEach-Object { Invoke-Command -ScriptBlock $expression}; Commented Nov 13, 2018 at 12:08

2 Answers 2

7

With Powershell core (6.2.3 at the time of this writing), you can install the sql server module like so:

Install-Module -Name SqlServer

and then you can call invoke-sqlcmd

If you want to know more about it: http://sqlvariant.com/2019/03/invoke-sqlcmd-is-now-available-cross-platform-in-the-sqlserver-module/

Sign up to request clarification or add additional context in comments.

1 Comment

This also worked for me running PowerShell Core on Windows.
0

In PowerShell Core, the sqlserver module doesn't export any of the Invoke-* cmdlets. However, there are the Read-SQLTableData and Read-SQLViewData cmdlets that appear to work in a similar fashion if you've already created views on the server side.

1 Comment

Seems it has since been fixed and available

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.