Powershell: How to capture output from the host

本秂侑毒 提交于 2019-12-10 15:45:13

问题


I am using powershell to automate some tasks related to checking out/merging in 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.


回答1:


Try something like this:

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




回答2:


The tee-object in PowerShell 2.0 allows you to pipe results to two sources. 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.



来源:https://stackoverflow.com/questions/3463211/powershell-how-to-capture-output-from-the-host

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!