[PowerShell] Exécuter un Insert Into dans une table SQL
Voici un simple script PowerShell permettant de faire un simple INSERT INTO dans une table dans SQL Serveur.
#################################################################
# Change data for your connection String #
#################################################################
$Serveur="PCSEVENAS\SQLEXPRESS" # Change for your Server Name #
$Database="MyDatabaseTest" # Change for your Database Name #
$Table="dbo.MyFirstTable" # Change for your Table Name #
#################################################################
$SQLConnect = new-object system.data.sqlclient.sqlconnection
$SQLConnect.connectionstring = "Server=$Serveur;database=$Database;trusted_connection=yes;"
# Open Connection to SQL Server
$SQLConnect.Open()
# End Connection String
# Loop for Insert Into
for ($C1=30;$C1 -lt 40;$C1++)
{
#################################################################
# Change your Columns Name [c1] [c2] #
#################################################################
$Requete = "INSERT INTO $Table ([c1],[c2])VALUES($C1,'Toto$C1')"
#################################################################
$command = New-object system.data.sqlclient.SqlCommand
$command.connection = $SQLConnect
$command.commandtext = $Requete
$command.Executenonquery()
}
# End Loop
# Close Connection to SQL Server
$SQLConnect.Close()
Voila le résultat dans SQL Server Management Studio:
