I wanted to run a sql script using powershell but getting an error "The term 'Invoke-sqlcmd' is not recognized as the name of a cmdlet, function, script file, or operable program. Check t he spelling of the name, or if a path was included, verify that the path is correct and try again."
I have found the below snippet from some website.but this is only for one sql command..But i wanted to run a sql script.
Could someone please help in modifying the below for sql script or any better suggestion ?
SQLServer = "ABCD\ABC"
$SQLDBName = "ABC_1223"
$SqlQuery = "select * from table"
$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString = "Server = $SQLServer; Database =$SQLDBName;uid=$SQLDBName;pwd= $pwd; Integrated Security = True"
$SqlCmd = New-Object System.Data.SqlClient.SqlCommand
$SqlCmd.CommandText = $SqlQuery
$SqlCmd.Connection = $SqlConnection
$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$SqlAdapter.SelectCommand = $SqlCmd
$DataSet = New-Object System.Data.DataSet
$SqlAdapter.Fill($DataSet)
$SqlConnection.Close()
clear
$DataSet.Tables[0]