SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Powershell


Powershell

Author
Message
dishantbmg
dishantbmg
Forum Newbie
Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)

Group: General Forum Members
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
Hakan Forsberg
Hakan Forsberg
Grasshopper
Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)

Group: General Forum Members
Points: 15 Visits: 317
Can you give a more detailed view of what you are trying to accomplish. Maybe there is an easier way for the same result...
Gary Gwyn
Gary Gwyn
Valued Member
Valued Member (64 reputation)Valued Member (64 reputation)Valued Member (64 reputation)Valued Member (64 reputation)Valued Member (64 reputation)Valued Member (64 reputation)Valued Member (64 reputation)Valued Member (64 reputation)

Group: General Forum Members
Points: 64 Visits: 572
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
dishantbmg
dishantbmg
Forum Newbie
Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)

Group: General Forum Members
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...
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)

Group: General Forum Members
Points: 84943 Visits: 41071
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.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Robert klimes
Robert klimes
SSCrazy
SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)

Group: General Forum Members
Points: 2661 Visits: 3421
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
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search