我有一个PowerShell脚本,我想将输出重定向到一个文件。问题是我不能改变这个脚本的调用方式。所以我不能:
.\MyScript.ps1 > output.txt
如何重定向PowerShell脚本执行期间的输出?
我有一个PowerShell脚本,我想将输出重定向到一个文件。问题是我不能改变这个脚本的调用方式。所以我不能:
.\MyScript.ps1 > output.txt
如何重定向PowerShell脚本执行期间的输出?
当前回答
我认为你可以修改MyScript.ps1。然后试着像这样改变它:
$(
Here is your current script
) *>&1 > output.txt
我刚刚用PowerShell 3试了一下。你可以像内森·哈特利的回答一样使用所有的重定向选项。
其他回答
要在你的脚本中嵌入它,你可以这样做:
Write-Output $server.name | Out-File '(Your Path)\Servers.txt' -Append
这样应该可以了。
我认为你可以修改MyScript.ps1。然后试着像这样改变它:
$(
Here is your current script
) *>&1 > output.txt
我刚刚用PowerShell 3试了一下。你可以像内森·哈特利的回答一样使用所有的重定向选项。
如果你想从命令行执行,而不是内置到脚本本身,使用:
.\myscript.ps1 | Out-File c:\output.csv
powershell ".\MyScript.ps1" > test.log
如果你的情况允许,有一个可能的解决方案:
重命名MyScript。ps1到TheRealMyScript.ps1 创建一个新的MyScript。Ps1看起来像: \ TheRealMyScript。Ps1 > output.txt