惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

美团技术团队
博客园_首页
量子位
F
Fortinet All Blogs
L
LangChain Blog
The Cloudflare Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
大猫的无限游戏
大猫的无限游戏
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
V
Visual Studio Blog
Blog — PlanetScale
Blog — PlanetScale
博客园 - 叶小钗
Apple Machine Learning Research
Apple Machine Learning Research
博客园 - Franky
G
Google Developers Blog
阮一峰的网络日志
阮一峰的网络日志
T
Tailwind CSS Blog
WordPress大学
WordPress大学
宝玉的分享
宝玉的分享
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
有赞技术团队
有赞技术团队
U
Unit 42
IT之家
IT之家

博客园 - Coding让生活更美好

Entity Framework search sequnce Runtime Complexity of .NET Generic Collection update the UI property cross thread 打通技术的任督二脉 windbg symbol path 一个文件夹可以link 到另外一个文件夹 Series on Coded UI Test Extensibility Launching the Debugger Automatically 修复软件安装问题和软件卸载问题 How to do code coverage test for windows service EF6 大数据框架整理 技术要点整理 修改table 设置默认值 PowerShell - Special Characters And Tokens To open the project created by previous version of Visual Studio Powershell 转义字符 asp.net Login控件基本属性及事件说明 if test project can't be opened in devenv
获取Powershell write-host 的内容
Coding让生活更美好 · 2012-09-13 · via 博客园 - Coding让生活更美好

如果是在powershell script 内部,想要将write-host的内容输入到一个文本文件,可以使用powershell的输出重定向功能,

可以使用以下方法重定向输出:  

- 使用 Out-File cmdlet,该 cmdlet 将命令输出发送至一个文本文件。通常在需要使用 Out-File cmdlet 的参数(例如 Encoding、Force、Width 或 NoClobber 参数)时 使用该 cmdlet。

- 使用 Tee-Object cmdlet,该 cmdlet 将命令输出发送至一个文本文件,然后将其发送至 管道。

- 使用 Windows PowerShell 重定向运算符。 Windows PowerShell 重定向运算符如下。

运算符 说明 示例 -------- ---------------------- ------------------------------

> 将输出发送到指定文件。 get-process > process.txt

>> 将输出追加到指定文件的内容。 dir *.ps1 >> scripts.txt

2> 将错误发送到指定文件。 get-process none 2> errors.txt

2>> 将错误追加到指定文件的内容。 get-process none 2>> save-errors.txt

2>&1 将错误发送到成功输出流。 get-process none, powershell 2>&1

在不修改script文件的前提下,如果想要获取script文件里write-host的输出,那么该怎么办呢? 

实际上powershell 没有提供再次读取write-host的功能,只能使用windows command 的输出功能来达到这个目的。

比如:   powershell.exe C:\test.ps1 >> C:\test.log 

这句话的意思是调用powershell 来执行test.ps1,然后将所有输出内容重定向到c:\test.log