The term 'EXEC' is not recognized as the name of a cmdlet Powershell

5.8k views Asked by At

I have an SSIS dtsx package that I want to run using PowerShell. Below is what I am running in powershell.

EXEC xp_cmdshell '"C:\Program Files\Microsoft SQL Server\130\DTS\Binn\DTExec.exe" /f "F:\SqlExport\New package.dtsx"'

Unfortunately I get the below error, and I dont know why

EXEC : The term 'EXEC' is not recognized as the name of a cmdlet, function,
script file, or operable program. Check the spelling of the name, or if a path
was included, verify that the path is correct and try again.
At line:1 char:1
+ EXEC xp_cmdshell '"C:\Program Files\Microsoft SQL Server\130\DTS\Binn ...
+ ~~~~
    + CategoryInfo          : ObjectNotFound: (EXEC:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

I have enabled the xp_cmdshell in SQL Server using below command as well

1

There are 1 answers

0
mklement0 On BEST ANSWER

EXEC xp_cmdshell is a T-SQL statement for launching shell commands via cmd.exe.

I presume the intent is to have PowerShell execute the command that starts with executable path "C:\Program Files\Microsoft SQL Server\130\DTS\Binn\DTExec.exe", launched from your T-SQL script via EXEC xp_cmdshell[1].

Instead, your error message implies that PowerShell executed the entire line, which predictably failed: PowerShell has no EXEC command (and there is no external program by that name on your system).

That said, PowerShell is neither needed here, nor would it enter the picture, if your EXEC xp_cmdshell line had been executed by SQL Server (via a T-SQL script).

In fact, your T-SQL command should work as-is (invocation of an external program with arguments, via cmd.exe), if properly executed by SQL Server.


[1] Update: If the intent is simply to launch the command from PowerShell

& "C:\Program Files\Microsoft SQL Server\130\DTS\Binn\DTExec.exe" /f "F:\SqlExport\New package.dtsx"`

Note the need for &, the call operator, to tell PowerShell that the double-quoted string that follows is the name of an executable to invoke.