I am using powershell to run a .sql file and I need to log the out put to a txt or csv file.Trouble is I am not getting the error messages displayed in the powershell consol.I think an example is called for :-)This script is saved in C:\temp as mysqlfile.sql[code="sql"]use tempdb goif exists (select name from sys.objects where object_id = object_id('PowershellTest'))begindrop table dbo.PowershellTest1end gocreate table dbo.PowershellTest (ColID int identity (1,1),ColDate datetime)go insert into dbo.PowershellTest(ColDate)select GETDATE()goPRINT 'Hello'select * from dbo.PowershellTestgo[/code]:NOTE the script is built to create two errors on the second run- drop table dbo.PowershellTest1 fails as it does not exist- create table dbo.PowershellTest fails as it already existsand I call it from powershell with this[code="other"]invoke-sqlcmd -inputfile "C:\Temp\mysqlfile.sql" -serverinstance "<serverinstance>" -database "Tempdb" -verbose 4>&1 | out-file C:\Temp\ReleaseLog.txt [/code]from the first run my releaselog.txt looks like this[code="other"]Der Datenbankkontext wurde auf 'tempdb' geändert.Hello ColID ColDate ----- ------- 1 07.10.2014 16:43:29 [/code]on the second run it looks like this[code="other"]Der Datenbankkontext wurde auf 'tempdb' geändert.Hello ColID ColDate ----- ------- 1 07.10.2014 16:43:29 2 07.10.2014 16:46:21 [/code]and the consol is displaying the expected errors [code="other"]invoke-sqlcmd : Löschen des Tabelle-Objekts 'dbo.PowershellTest1' ist nicht möglich, weil das Objekt nicht vorhanden ist oder Sie nicht die erforderliche Berechtigung haben.In Zeile:2 Zeichen:1+ invoke-sqlcmd -inputfile "C:\Temp\mysqlfile.sql" -serverinstance "<instancename>" ...+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [Invoke-Sqlcmd], SqlPowerSh ellSqlExecutionException + FullyQualifiedErrorId : SqlError,Microsoft.SqlServer.Management.PowerShel l.GetScriptCommand invoke-sqlcmd : In der Datenbank ist bereits ein Objekt mit dem Namen 'PowershellTest' vorhanden.In Zeile:2 Zeichen:1+ invoke-sqlcmd -inputfile "C:\Temp\mysqlfile.sql" -serverinstance "<instancename>" ...+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [Invoke-Sqlcmd], SqlPowerSh ellSqlExecutionException + FullyQualifiedErrorId : SqlError,Microsoft.SqlServer.Management.PowerShel l.GetScriptCommand[/code]I am using Powershell V4 ,sql server 2008R2 and working from a Windows 7 pcMany thanks in advanceIan
↧