I am trying to get the output of the queries executed in SQLSErver database using sql script through Powershell. I am using Invoke-SQLCMD to get this done with verbose parameter. Its my understanding that using of "verbose" should capture all messages, error messages if any and all query results in a output log file . But when I check the log file, I get only the output of the first query from the SQL Script.
I even checked the script commands are executing successfully but the output is not logged. Can anyone help on this.
My Invoke-sqlcmd Command is
Invoke-Sqlcmd -ServerInstance MSSERVER -Database BikeStores -InputFile testsql.sql -Verbose *>> "test.log"
My sql File contains following code
use[BikeStores]
select name from sys.databases; GO
select * from [BikeStores].[production].[brands]; GO
SELECT brand_id,brand_name INTO [BikeStores].[production].[test_table2] FROM [BikeStores].[production].[brands]; GO select * from [BikeStores].[production].[test_table2];
update production.test_table2 set brand_name='xxx' where brand_id=1;
select * from [BikeStores].[production].[test_table2];
delete from BikeStores.production.test_table where brand_id=1; drop table BikeStores.production.test_table2;
My Output is as below.
name
----
master
tempdb
model
msdb
BikeStores
I expect the code to put the following lines. Ideally speaking, the script can contain any DML or DDL statements, it should capture the query and output messages, results.
name
----
master
tempdb
model
msdb
BikeStores
brand_id brand_name
1 Electra
2 Haro
3 Heller
4 Pure Cycles
5 Ritchey
6 Strider
7 Sun Bicycles
8 Surly
9 Trek
SELECT brand_id,brand_name INTO [BikeStores].[production].[test_table2] FROM [BikeStores].[production].[brands];
GO
(9 rows affected)
Completion time: 2022-08-13T17:03:51.1934168+10:00
select * from [BikeStores].[production].[test_table2];
brand_id brand_name
1 Electra
2 Haro
3 Heller
4 Pure Cycles
5 Ritchey
6 Strider
7 Sun Bicycles
8 Surly
9 Trek
update production.test_table2
set brand_name='xxx'
where brand_id=1;
(1 row affected)
Completion time: 2022-08-13T17:04:40.6506035+10:00
select * from [BikeStores].[production].[test_table2];
brand_id brand_name
1 xxx
2 Haro
3 Heller
4 Pure Cycles
5 Ritchey
6 Strider
7 Sun Bicycles
8 Surly
9 Trek
delete from BikeStores.production.test_table where brand_id=1;
(0 rows affected)
Completion time: 2022-08-13T17:05:11.4045005+10:00
drop table BikeStores.production.test_table2;
Commands completed successfully.
Completion time: 2022-08-13T17:05:37.9947206+10:00