我在powershell中有一個字符串,它包含一個本地sqlcmd命令。該命令本身可以在cmd.exe中成功執行。我很難在PowerShell中執行它們。任何人都可以幫忙謝謝。如何從powershell執行sqlcmd?
這是sql.sql
select @@servername
go
select @@servicename
這是結果,當我從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腳本調用SQLCMD命令。
$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
相關參數:http://stackoverflow.com/questions/1015038/powershell-sqlcmd – 2013-01-31 12:35:02