开发者

SQL Server 2008: how to get the "Messages" tab (in Management Studio) output with powershell

I am using invoke-sqlcmd to manage my databases, it works fine. I save the output of the "Results" tab in Management Studio to a log file.

When I do a BACKUP DATABASE, the output is done on the "Messages" tab and I would like to save it in the logfile.

Is there a way to get that output ? (o开发者_开发问答r a table in SQL Server 2008 that stores the backup progression and results ?)

The SQL Server 2008 logs contain something but it is not as verbose as the "Messages" tab.


Invoke-Sqlcmd does not display messages, such as the output of PRINT statements, unless you specify the Windows PowerShell -Verbose common parameter. For example:

Invoke-Sqlcmd -Query "PRINT N'abc';" -Verbose

Source


A working example if it can help someone :

$ps = [PowerShell]::Create()
[ref]$e = New-Object System.Management.Automation.Runspaces.PSSnapInException
$ps.Runspace.RunspaceConfiguration.AddPSSnapIn( "SqlServerCmdletSnapin100", $e ) | Out-Null
$ps.AddCommand( "Invoke-Sqlcmd" ).AddParameter( "Verbose" ).AddParameter( "ServerInstance", "localhost\SQLEXPRESS2K8" ).AddParameter( "Query", "BACKUP DATABASE xxx TO DISK = N'c:\tmp\xxx.bak' WITH FORMAT, STATS = 10" )
$ps.Invoke()
$ps.Streams.Verbose | % { $_.Message} | Out-File c:\tmp\ps002.txt
cat c:\tmp\ps002.txt
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜