Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Powershell Expand / Collapse
Author
Message
Posted Wednesday, June 25, 2014 8:18 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, July 1, 2014 2:56 AM
Points: 2, Visits: 11
Hi,

As we know there are two output windows in sql server management studio like 'Results' and 'Messages',
I want to capture that 'Messages' window into a log file using powershell program .
For ex:- into a log file it should display like

(1 row(s) affected)

(1 row(s) affected)


Can anyone help me out with this one, Thanks in advance

Dishant jain
Post #1585919
Posted Wednesday, June 25, 2014 11:59 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, December 11, 2014 12:08 AM
Points: 9, Visits: 292
Can you give a more detailed view of what you are trying to accomplish. Maybe there is an easier way for the same result...
Post #1586034
Posted Wednesday, June 25, 2014 12:48 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Wednesday, December 3, 2014 9:03 AM
Points: 25, Visits: 283
When using the Invoke-Sqlcmd, include the -Verbose option. That will output the "messages" tab from SSMS.


PS U:\> Add-PSSnapin sqlservercmdletsnapin100
PS U:\> Invoke-Sqlcmd -Query "select 'test'; PRINT N'123';" -Verbose

Column1
-------
test
VERBOSE: 123


PS U:\>


Blog: http://sqlexchange.wordpress.com
Post #1586057
Posted Thursday, June 26, 2014 12:10 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, July 1, 2014 2:56 AM
Points: 2, Visits: 11
Actually i wrote a powershell program which is taking an input file containing some scripts, i want to capture message window of SSMS into a log file. How can achieve that...
Post #1586227
Posted Thursday, June 26, 2014 8:35 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Yesterday @ 7:27 AM
Points: 35,769, Visits: 32,437
dishantbmg (6/26/2014)
Actually i wrote a powershell program which is taking an input file containing some scripts, i want to capture message window of SSMS into a log file. How can achieve that...


I could be wrong but I'm pretty sure it (code controls SSMS) can't be done automatically where you have control over what the name of the file is. You CAN do it manually just before you run your code by selecting "Results to File" instead of "Results to Grid" or "Results to Text".

An alternative might be to use the DOS redirection character for SQLCmd or pipe the output to a file when using PoSH.


--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1586708
Posted Friday, June 27, 2014 3:18 PM


UDP Broadcaster

UDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP Broadcaster

Group: General Forum Members
Last Login: Yesterday @ 10:03 AM
Points: 1,499, Visits: 2,814
Are the queries you are trying to capture rows affected from selects or insert/update/delete?

If they are modification you are trying to get affected rows for something like this may work for you.
function Invoke-Sqlcmd2
{
param(
[string]$ServerInstance,
[string]$Database,
[string]$Query,
[Int32]$QueryTimeout=120
)

$conn=new-object System.Data.SqlClient.SQLConnection
$conn.ConnectionString="Server={0};Database={1};Integrated Security=True" -f $ServerInstance,$Database
$conn.Open()
$cmd=new-object system.Data.SqlClient.SqlCommand($Query,$conn)
$cmd.CommandTimeout=$QueryTimeout
$cmd.executenonquery()
$conn.Close()

}


Invoke-Sqlcmd2 -ServerInstance "instancename" -database "dbname" -Query "update foo set id = 0 where id < 100"



Bob
-----------------------------------------------------------------------------
How to post to get the best help
Post #1587050
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse