我在 PowerShell 中有一个字符串,其中包含本地 sqlcmd 命令。该命令在 cmd.exe 中本身可以成功执行,但我在 PowerShell 中无法执行它。 有人能帮忙吗?谢谢。
这是 sql.sql
select @@servername
go
select @@servicename
当我从cmd.exe执行sqlcmd命令时,这就是结果。
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脚本。
$sql = @"
sqlcmd -S "(local)\instance1" -U a -P a -i "c:\temp\sql.sql"
"@
Invoke-Command $sql
当我执行这个 PowerShell 脚本时,我得到了以下错误。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