Output message from invoke-sqlcmd required

  • 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

    use tempdb

    go

    if exists (select name from sys.objects where object_id = object_id('PowershellTest'))

    begin

    drop table dbo.PowershellTest1

    end

    go

    create table dbo.PowershellTest (ColID int identity (1,1),ColDate datetime)

    go

    insert into dbo.PowershellTest(ColDate)

    select GETDATE()

    go

    PRINT 'Hello'

    select * from dbo.PowershellTest

    go

    :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 exists

    and I call it from powershell with this

    invoke-sqlcmd -inputfile "C:\Temp\mysqlfile.sql" -serverinstance "<serverinstance>" -database "Tempdb" -verbose 4>&1 | out-file C:\Temp\ReleaseLog.txt

    from the first run my releaselog.txt looks like this

    Der Datenbankkontext wurde auf 'tempdb' geändert.

    Hello

    ColID ColDate

    ----- -------

    1 07.10.2014 16:43:29

    on the second run it looks like this

    Der Datenbankkontext wurde auf 'tempdb' geändert.

    Hello

    ColID ColDate

    ----- -------

    1 07.10.2014 16:43:29

    2 07.10.2014 16:46:21

    and the consol is displaying the expected errors

    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

    I am using Powershell V4 ,sql server 2008R2 and working from a Windows 7 pc

    Many thanks in advance

    Ian

  • You may be out of luck hear as it appears that they supplied a fix in SQL 2014 CTP2 as said here.

    Gaz

    -- Stop your grinnin' and drop your linen...they're everywhere!!!

  • Thanks Gary.

  • Sorry that I couldn't be more helpful.

    Gaz

    -- Stop your grinnin' and drop your linen...they're everywhere!!!

  • Solved 😀

    I changed the verbose to -verbose *>

    invoke-sqlcmd -inputfile "C:\Temp\mysqlfile.sql" -serverinstance "ServerInstance" -database "Tempdb" -verbose *> C:\Temp\ReleaseLog.txt

    now my ReleaseLog.TXT hold all the information

    Der Datenbankkontext wurde auf 'tempdb' geändert.

    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:1 Zeichen:1

    + invoke-sqlcmd -inputfile "C:\Temp\mysqlfile.sql" -serverinstance "ServerInstance" ...

    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo : InvalidOperation: (:) [Invoke-Sqlcmd], SqlPowerShellSqlExecutionException

    + FullyQualifiedErrorId : SqlError,Microsoft.SqlServer.Management.PowerShell.GetScriptCommand

    invoke-sqlcmd : In der Datenbank ist bereits ein Objekt mit dem Namen 'PowershellTest' vorhanden.

    In Zeile:1 Zeichen:1

    + invoke-sqlcmd -inputfile "C:\Temp\mysqlfile.sql" -serverinstance "ServerInstance" ...

    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo : InvalidOperation: (:) [Invoke-Sqlcmd], SqlPowerShellSqlExecutionException

    + FullyQualifiedErrorId : SqlError,Microsoft.SqlServer.Management.PowerShell.GetScriptCommand

    Hello

    ColID ColDate

    ----- -------

    1 07.10.2014 16:43:29

    2 07.10.2014 16:46:21

    3 08.10.2014 09:21:28

    4 08.10.2014 09:54:29

    6 08.10.2014 09:55:59

    8 08.10.2014 09:58:08

    13 08.10.2014 16:30:04

    5 08.10.2014 09:55:11

    7 08.10.2014 09:56:56

    10 08.10.2014 10:11:45

    9 08.10.2014 10:09:06

    11 08.10.2014 10:11:55

    12 08.10.2014 10:12:22

    14 08.10.2014 16:32:47

    in the end I found my answer here

    http://technet.microsoft.com/en-us/library/hh847746.aspx

  • Thanks for posting the answer to your own question (it is amazing how many don't). I really appreciate it.

    If you can please can you mark your answer as the solution to aid others.

    Many thanks,

    Gaz

    -- Stop your grinnin' and drop your linen...they're everywhere!!!

Viewing 6 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply