如果是在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 

 

posted on 2012-09-13 10:58  Coding让生活更美好  阅读(6056)  评论(0编辑  收藏  举报