To control how generated statements are going to be executed, you need to set the SqlExecutionMode explicitly on the ConnectionContext object as follows:
serverInstance.ConnectionContext.SqlExecutionModes = SqlExecutionModes.CaptureSql;
Dim scriptBatches As StringCollection
scriptBatches = serverInstance.ConnectionContext. CapturedSql.Text;
StringCollection scriptBatches = serverInstance.ConnectionContext.CapturedSql.Text;
Tip
The reason captured T-SQL is retrieved as a StringCollection is to avoid the need for parsing different batches, as each batch is represented by its own string entry in the collection. Actually, the SMO ServerConnection object enables you to execute a collection of batches directly as follows:
serverInstance.ConnectionContext.ExecuteNonQuery (scriptBatches);