简体   繁体   English

Powershell:如何捕获主机的输出

[英]Powershell: How to capture output from the host

I am using powershell to automate some tasks related to checking out/merging in TFS. 我正在使用powershell来自动执行与TFS中的签出/合并相关的一些任务。 When I call 我打电话的时候

tf get * /recurse

I get a bunch of data scrolling by about the files that are getting checked out. 关于正在检出的文件,我得到一堆数据滚动。 The last line generated by this command (assuming its success) is one telling the checkin number. 此命令生成的最后一行(假设其成功)是一个告诉签入号码的行。 I would like to parse this out so it can be used later on in my script. 我想解析它,以便以后可以在我的脚本中使用它。

I know that I can do something like 我知道我可以做类似的事情

$getOutput = tf get * /recurse

but then the output is suppressed entirely and I want the output of that command to be scrolled in realtime. 但随后输出被完全抑制,我希望该命令的输出实时滚动。 I would basically like to grab everything that just got sent to the output buffer. 我基本上想要抓住刚刚发送到输出缓冲区的所有内容。

Try something like this: 尝试这样的事情:

tf get * /recurse | 获得* / recurse | tee-Object -Variable getOutput tee-Object -Variable getOutput

The tee-object in PowerShell 2.0 allows you to pipe results to two sources. PowerShell 2.0中的tee对象允许您将结果传递给两个源。 If you leave the second source empty, the results go to the console. 如果将第二个源保留为空,则结果将转到控制台。

ls | tee-object -filePath directoryListing.txt

This will write the directory listing to both the console and a text file. 这会将目录列表写入控制台和文本文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM