我正在尝试捕获Powershell中的Invoke-Sqlcmd的详细输出。任何人有任何想法来做这件事:
即
Invoke-Sqlcmd -Query "PRINT 'Hello World!';" -ServerInstance $Server -verbose > D:\SqlLog.txt
SqlLog.txt文件应该包含文本"Hello World!“
发布于 2010-12-24 08:34:30
由于通过PowerShell主机的本机构造很难捕获详细的输出,因此您可以始终使用对PowerShell object的编程访问。然后,您可以访问这五种不同的信息流:
> $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( "Query", "Print 'hello world'" ).AddParameter( "Verbose" )
> $ps.Invoke()
> $ps.Streams
Error : {}
Progress : {}
Verbose : {hello world}
Debug : {}
Warning : {}
> $ps.Streams.Verbose | % { $_.Message | Out-File -Append D:\SqlLog.txt }
> cat D:\SqlLog.txt
hello world
发布于 2014-03-01 00:59:34
根据Capture Warning, Verbose, Debug and Host Output via alternate streams的说法
...if我想在脚本中捕获详细的输出:
stop-process -n vd* -verbose 4>&1 > C:\Logs\StoppedProcesses.log
所以,你会做类似这样的事情
(Invoke-Sqlcmd -Query "PRINT 'Hello World!';" -ServerInstance $Server -verbose) 4> c:\temp\myoutput.txt
其中4是“详细”流。
发布于 2010-12-23 01:06:41
请尝试:
Invoke-Sqlcmd -Query "PRINT 'Hello World!';" -ServerInstance $Server -verbose > D:\SqlLog.txt 2>&1
I found it at
https://stackoverflow.com/questions/4511498
复制相似问题