powershell - 使用命令提示符将 PowerShell 脚本的输出重定向到文件
问题描述
如何在 Windows 10 中使用 cmd 将 PowerShell 5.0 脚本的输出重定向到文件?我尝试了以下方法:
powershell ".\myscript.ps1 | Out-File outfile.txt"
和:
powershell .\myscript.ps1 > outfile.txt
无济于事。已outfile.txt
创建但仍为空。命令提示符窗口以管理员权限运行。
在我使用的脚本中:
Write-Host $MyVar
Write-Host $SomeOtherVar
将值输出到屏幕。
解决方案
使用特定的PowerShell 重定向运算符:(
它们似乎也可以在 (cmd) 命令提示符下工作)
重定向成功流(1>
):
powershell .\myscript.ps1 1> outfile.txt
重定向所有流 ( *>
):
powershell .\myscript.ps1 *> outfile.txt
推荐阅读
- python - 框架旁边的文字 | 特金特
- javascript - REGEX - 用于字母数字/数字之间可能有斜线
- java - 从数组中删除重复项(深拷贝)Java
- c# - C# 存储库 CSV 问题。这段代码究竟做了什么?
- reactjs - 适当的助手或自定义 React 钩子来添加和初始化 Firebase
- c# - 在 GET 处接收客户端项目的 index.html,而不是来自服务器的 json 数据
- python - 基于 Python Pandas 中两列中的值的数据框修改?
- kubernetes - 可以从 OpenShift 模板文件完成 Kubernetes 部署吗?
- php - 如何将来自不同 mySQL 数据库的表与 PHP 进行比较?
- java - 一个线程的多个活动处理程序