首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Powershell Invoke-Sqlcmd捕获详细输出

Powershell Invoke-Sqlcmd捕获详细输出
EN

Stack Overflow用户
提问于 2010-12-23 00:50:05
回答 11查看 43.1K关注 0票数 15

我正在尝试捕获Powershell中的Invoke-Sqlcmd的详细输出。任何人有任何想法来做这件事:

代码语言:javascript
运行
复制
Invoke-Sqlcmd  -Query "PRINT 'Hello World!';" -ServerInstance $Server -verbose  > D:\SqlLog.txt

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

EN

回答 11

Stack Overflow用户

回答已采纳

发布于 2010-12-24 08:34:30

由于通过PowerShell主机的本机构造很难捕获详细的输出,因此您可以始终使用对PowerShell object的编程访问。然后,您可以访问这五种不同的信息流:

代码语言:javascript
运行
复制
> $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
票数 11
EN

Stack Overflow用户

发布于 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

所以,你会做类似这样的事情

代码语言:javascript
运行
复制
(Invoke-Sqlcmd -Query "PRINT 'Hello World!';" -ServerInstance $Server -verbose) 4> c:\temp\myoutput.txt

其中4是“详细”流。

票数 17
EN

Stack Overflow用户

发布于 2010-12-23 01:06:41

请尝试:

代码语言:javascript
运行
复制
Invoke-Sqlcmd  -Query "PRINT 'Hello World!';" -ServerInstance $Server -verbose  > D:\SqlLog.txt  2>&1

I found it at

票数 9
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4511498

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档