Ce mémo a été publié le 5 août 2016 et peut contenir des informations qui peuvent être incomplètes, non mises à jour voir erronées du fait de son ancienneté.
N'hésitez pas à compléter votre recherche sur des articles plus récents.
Mémo pour exécuter une procédure stockée MS SQL Server en lui passant des paramètres à partir d’un script PowerShell.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
function exec_ps($sqlserver_psname, $ps_PARAM1, $ps_PARAM2) { $connString = "Data Source=" + $sqlserver_servername + ";Initial Catalog=" + $sqlserver_database + ";User Id=$sqlserver_user; Password=$sqlserver_pwd;" $SQLConnection = New-Object System.Data.SqlClient.SqlConnection $SQLConnection.ConnectionString = $ConnString $SQLCmdString = "dbo.$sqlserver_psname" $SQLCmdTimeout = 120 $SQLCmd = New-Object System.Data.SqlClient.SqlCommand $SQLCmd.CommandType = [System.Data.CommandType]::StoredProcedure $SQLCmd.CommandText = $SQLCmdString $SQLCmd.CommandTimeout = $SQLCmdTimeout $SQLCmd.Connection = $SQLConnection $SQLCmd.Parameters.AddWithValue("@PARAM1", $ps_PARAM1) | Out-Null $SQLCmd.Parameters.AddWithValue("@PARAM2", $ps_PARAM2) | Out-Null $SQLConnection.Open() $SQLCmd.ExecuteNonQuery() | out-null $SQLConnection.Close() } |