Powershell Invoke-Sqlcmd 捕获详细输出 [英] Powershell Invoke-Sqlcmd capture verbose output

查看:99
本文介绍了Powershell Invoke-Sqlcmd 捕获详细输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从 Powershell 中的 Invoke-Sqlcmd 捕获详细输出.任何人都有这样做的想法:

I'm trying to capture the verbose output from the Invoke-Sqlcmd in Powershell. Anyone got any ideas to do this:

Invoke-Sqlcmd  -Query "PRINT 'Hello World!';" -ServerInstance $Server -verbose  > D:\SqlLog.txt

SqlLog.txt 文件应包含文本Hello World!"

The SqlLog.txt file should contain the text "Hello World!"

推荐答案

由于捕获详细输出不是通过 PowerShell 主机的本机构造可以轻松完成的事情,因此您始终可以使用对 PowerShell 对象.然后,您可以访问五种不同的信息流:

Since capturing verbose output is not something one can do easily through the native constructs of the PowerShell host, you can always use the programatic access to the PowerShell object. You can then gain access to the five different streams of information:

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

这篇关于Powershell Invoke-Sqlcmd 捕获详细输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆