0

I have been trying to develop a function that handles all the SQLCMD requirements for a complex process. This includes mixed connection modes (trusted or username/password), different databases, statement execution or script file execution, capturing output to variables or to a file for parsing, lots of variables pass-through and generating dynamic SQL to a file (using both select and print statements and very wide output). I had tried and discounted ADO.NET and had been testing between Invoke-SQLCMD and Start-Process -FilePath ...\SQLCmd.exe...

I found a reference to SQLCMD and I copied/pasted it in to powershell, modified it and it worked!

$result = sqlcmd -U uuuuu -P pppppp -i "D:\temp\testscript.sql" -v time="'time'" -S $server -d Master -w 3000

But what is it? When I do get-help SQLCMD, it only lists Invoke-SQLCMD and my functions. When I do get-alias sqlcmd it throws an error as it doesn't exist. I have a $sqlcmd variable but I'm not referencing it like that.

By the way, if anyone knows of a useful SQLCMD function that handles all this, happy to look.

5
  • 1
    learn.microsoft.com/en-us/sql/tools/… Commented Mar 15, 2022 at 23:49
  • 1
    It's an external/native executable application (same as ping.exe, notepad.exe or powershell.exe for that matter). The command you're looking for to discover this is Get-Command sqlcmd :) Commented Mar 16, 2022 at 0:13
  • I don't understand... you are "trying to develop a function that handles all the SQLCMD requirements" but you don't know what SQLCMD is? Where does this requirement come from? Why not just use SQLCMD.... end of story? Commented Mar 16, 2022 at 0:31
  • Excellent, thanks @MathiasR.Jessen. That's the info I needed. Commented Mar 16, 2022 at 1:49
  • 1
    @Nick.McDermaid - I was using Invoke-SQLCmd and Start-Process ...SQLCMD... but I didn't know I could just use SQLCmd directly from powershell and what it was actually running. As Mathias explained, it's an external command which is what I thought but couldn't understand why I couldn't find any reference to it from Powershell. get-command is the answer. Commented Mar 16, 2022 at 1:51

1 Answer 1

1

As mentioned in the comments, the command you're looking for is Get-Command:

PS ~> Get-Command sqlcmd |Format-Table -AutoSize

CommandType Name       Version       Source
----------- ----       -------       ------
Application SQLCMD.EXE 15.0.1300.359 C:\Program Files\Microsoft SQL Server\

Under CommandType we can see that indeed it resolves to a native executable application, which is a valid command type in PowerShell (the same ping.exe, cmd.exe or even powershell.exe would be).

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

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.