Tôi có một chuỗi trong PowerShell, trong đó có một lệnh sqlcmd bản địa. Lệnh này có thể được thực thi thành công trong cmd.exe. Tôi gặp khó khăn trong việc thực hiện chúng trong PowerShell. Bất cứ ai cũng có thể giúp đỡ? Cảm ơn.Làm thế nào để thực hiện sqlcmd từ PowerShell?
Đây là sql.sql
select @@servername
go
select @@servicename
Đây là kết quả khi tôi thực hiện lệnh sqlcmd từ 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>
Đây là kịch bản PowerShell để gọi lệnh sqlcmd.
$sql = @"
sqlcmd -S "(local)\instance1" -U a -P a -i "c:\temp\sql.sql"
"@
Invoke-Command $sql
Khi tôi thực thi tập lệnh PowerShell này, tôi nhận được lỗi sau.
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
liên quan: http://stackoverflow.com/questions/1015038/powershell-sqlcmd –