Hi Guys,
I’m trying to stop the PS script execution when i get errors in SQL server database. I’m trying to select a table but if the table doesn’t exist in the database, script just throws the errors and continue the rest of the execution. Could you please help me on this ?
$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
$SqlCmd = New-Object System.Data.SqlClient.SqlCommand
$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$DataSet = New-Object System.Data.DataSet #-ErrorAction Stop
$SqlConnection.ConnectionString = “Server = DBATest; Database = master; Integrated Security = True”
$SqlCmd.CommandText = " use test
select employee from emp where firstname like ‘%ton%’"
$SqlCmd.Connection = $SqlConnection
$SqlAdapter.SelectCommand = $SqlCmd
$SqlAdapter.Fill($DataSet)|out-null
$rownum = $Dataset.Tables[0].Rows.Count
This is just a part of the script where i need to stop the execution and send an email with the error message when the table is not present in the database.
I’m using -ErrorAction Stop but its isn’t working or may be i don’t know where exactly i need use that function.
Thanks in advance