#simple script demonstrates the DB interaction $SQLServer = "Yourdbserver" # provide your database server hostname $SQLDBName = "YourDBName" $SqlConnection = New-Object System.Data.SqlClient.SqlConnection $SqlConnection.ConnectionString = "Server = $SQLServer; Database = $SQLDBName; User ID= YourUserID; Password= YourPassword" $SqlCmd = New-Object System.Data.SqlClient.SqlCommand $SqlCmd.CommandText = 'yourquery' $SqlCmd.Connection = $SqlConnection $SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter $SqlAdapter.SelectCommand = $SqlCmd $DataSet = New-Object System.Data.DataSet $SqlAdapter.Fill($DataSet) $SqlConnection.Close() #End :database Intraction clearNote: Whenever you open a DB connection with Powershell script or any other scripting language make sure you have closed the connection if not it would create some odd issues, one may run into null values in database records, so please close the connection when you done with interaction with DB. Another important thing is you must have all privileges to run the query before you run the script make sure you have opened it with an elevated account. Thank you for reading this article, if you have any questions please let us know. Thank you for visiting my site, for any scripts in these articles you are testing please make sure you have tested this script in our lower environment before you run in production.
Leave a Reply Cancel reply