如何从powershell执行sqlcmd? [英] How to execute sqlcmd from powershell?
问题描述
我在 powershell 中有一个字符串,其中包含一个本机 sqlcmd 命令.命令本身可以在cmd.exe中成功执行.我很难在 powershell 中执行它们.任何人都可以帮忙吗?谢谢.
I have a string in powershell, which contains a native sqlcmd command. The command itself can be executed successfully in cmd.exe. I have difficulty in executing them in powershell. Anyone can help? Thanks.
这是sql.sql
select @@servername
go
select @@servicename
这是我从cmd.exe执行sqlcmd命令时的结果
This is the result when I execute the sqlcmd command from cmd.exe
C:\Users\test>sqlcmd -S "(local)\instance1" -U a -P a -i "c:\temp\sql.sql"
--------------------------------------------------
thesimpsons\INSTANCE1
(1 rows affected)
--------------------------------------------------
INSTANCE1
(1 rows affected)
C:\Users\test>
这是调用 sqlcmd 命令的 powershell 脚本.
This is the powershell script to call the sqlcmd command.
$sql = @"
sqlcmd -S "(local)\instance1" -U a -P a -i "c:\temp\sql.sql"
"@
Invoke-Command $sql
当我执行这个 powershell 脚本时,出现以下错误.
When I execute this powershell script, I got the following error.
PS C:\TEMP> $sql = @"
sqlcmd -S "(local)\instance1" -U a -P a -i "c:\temp\sql.sql"
"@
Invoke-Command $sql
Invoke-Command : Parameter set cannot be resolved using the specified named parame
ters.
At line:5 char:15
+ Invoke-Command <<<< $sql
+ CategoryInfo : InvalidArgument: (:) [Invoke-Command], ParameterBin
dingException
+ FullyQualifiedErrorId : AmbiguousParameterSet,Microsoft.PowerShell.Commands
.InvokeCommandCommand
推荐答案
要调用 Win32 可执行文件,您需要使用调用运算符 &
,如下所示:
To call a Win32 executable you want to use the call operator &
like this:
& sqlcmd -S "(local)\instance1" -U a -P a -i "c:\temp\sql.sql"
这篇关于如何从powershell执行sqlcmd?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!