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

I use invoke-sqlcmdto manage my databases, it works great. I save the output of the Results tab in Management Studio to a log file.

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

Is there any way to get this conclusion? (or a table in SQL Server 2008 that stores backup results and results?)

SQL Server 2008 logs contain something, but it's not as much as the Messages tab.

+3
source share
2 answers

Invoke-Sqlcmd , , Windows PowerShell - . :

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

+3

, -:

$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
+2

All Articles