2

I had this sqlcmd snippet working in batch but when I converted to powershell, it broke. This is what I am attempting to call using powershell

Sqlcmd -S localhost\instance -d database -U username -P password -i "sqlQuery.sql" -s "," > \sqlOutput.csv -I -W -k

I tried invoke-sqlcmd but i get the error "The term 'invoke-sqlcmd' is not recognized as the name of a cmdlet, function, script file, or operable program"

In regards to the comment below, I have loaded the snapins required. This did help but I am now getting this error. "Invoke-sqlcmd : Parameter cannot be processed because the parameter name 's' is ambiguous.

If I put it in single quotes...

invoke-Sqlcmd '-S localhost\instance -d database -U username -P password -i "sqlQuery.sql" -s "," > \sqlOutput.csv -I -W -k'

I get the error "A network-related or instance-specific error occurred while establishing a connectiong to sql server. Which doesn't make sense to me as the same credentials i am using here (on the same box) work when called from batch.

2
  • To call Invoke-Sqlcmd you have to have loaded the SQL snapin. Commented Apr 6, 2012 at 17:36
  • The parameters of sqlcmd.exe and invoke-sqlcmd are not the same. see help invoke-sqlcmd -full for a list of parameters and example usage Commented Apr 7, 2012 at 1:20

2 Answers 2

2

You have to first call sqlps.exe from powershell or command prompt (assuming you have the SQL Server snap-ins for Powershell installed).

Once you do that, execute get-help Invoke-Sqlcmd to find your way around.

To your question, its not -S, but -ServerInstance

 PS SQLSERVER:\> Invoke-Sqlcmd -ServerInstance "srvrName" -Database "master" -Username "user123" -Password "x$34fth" 
-InputFile "C:\Users\testUser\Desktop\test.sql" | Out-File "C:\Users\testUser\Desktop\out.txt"

You can also use the MSDN documentation

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

Comments

0

Goto

  • Windows Explorer 
  • Right Click Computer 
  • Properties 
  • Advanced System Settings 
  • Environment Variables

Make sure PSModulePath is set with following string.

C:\Program Files (x86)\Microsoft SQL Server\110\Tools\PowerShell\Modules

Comments

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.