希望这不是重复的。我汇总了去年左右遇到的众多解决方案,以了解自己的所在。这对我来说都是相对较新的,我正在寻找最安全有效的解决方案。当我运行此命令时,什么也没发生。预期的结果是执行存储过程。
$Server = 'Server Name'
$database = 'DBName'
$userName = 'un'
$password = 'pw'
$Name = 'Name'
$Job = '15'
$Logs = Get-Content -Path $global:LOGFILE
$StartTime ='time'
$End = 'End'
$Connection = New-Object System.Data.SQLClient.SQLConnection
$Connection.ConnectionString = "Server=$('$Server');Database=$('$Database');trusted_connection=true;User Id=$('$userName');Password=$('$password')"
$Connection.Open()
$Command = New-Object System.Data.SQLClient.SQLCommand
$Command.Connection = $Connection
$Command.CommandText ="EXEC dbo.UpdateOutput @Name,@Job,@StartTime,@End,@Status,@Logs"
$Command.Parameters.AddWithValue("@Name", $Name)| Out-Null
$Command.Parameters.AddWithValue("@Job", $Job)| Out-Null
$Command.Parameters.AddWithValue("@Start", $StartTime)| Out-Null
$Command.Parameters.AddWithValue("@End", $End)| Out-Null
$Command.Parameters.AddWithValue("@Status", $Status)| Out-Null
$Command.Parameters.AddWithValue("@Logs", $Logs)| Out-Null
$Command.ExecuteNonQuery()
$Connection.Close()
最佳答案
除去ConnectionString行中的撇号,如下所示:
$Connection.ConnectionString = "Server=$($Server);Database=$($Database);trusted_connection=true;User Id=$($userName);Password=$($password)"
关于sql-server - 如何从Powershell执行存储过程?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56135960/