如何从标准输出中分离警告消息/导致powershell重定向?

如何从标准输出中分离警告消息/导致powershell重定向?,powershell,cmd,warnings,stdout,batch-processing,Powershell,Cmd,Warnings,Stdout,Batch Processing,我正在尝试在PowerShell中编写一个简单的测试脚本,以便在中获得两个单独的文本文件,一个用于警告,另一个用于标准结果 下面是我的简单输出.ps1脚本: Write-Output "This is result" Write-Warning "This is warning" 我正在尝试使用下面的命令使用.bat文件运行output.ps1: PowerShell.exe-Command“&”%$dp0output.ps1”3>warning.txt>results.txt 暂停 但我的

我正在尝试在PowerShell中编写一个简单的测试脚本,以便在中获得两个单独的文本文件,一个用于警告,另一个用于标准结果

下面是我的简单
输出.ps1
脚本:

Write-Output "This is result"
Write-Warning "This is warning"
我正在尝试使用下面的命令使用.bat文件运行
output.ps1

PowerShell.exe-Command“&”%$dp0output.ps1”3>warning.txt>results.txt
暂停
但我的问题是
warning.txt
完全为空,而警告和结果都写入
results.txt

这是结果
警告:这是警告
我在将警告和错误分隔为两个文件的
PowerShell.exe-Command“&“%$dp0output.ps1”3>warning.txt>results.txt
中缺少什么


编辑:修改以查看标准错误,
2>
正在工作

即使我使用
2>error.txt将标准错误重定向到单独的文件

PowerShell.exe-Command“&”%$dp0output.ps1”2>error.txt 3>warning.txt>results.txt
如果我修改
output.ps1
以以下方式生成错误

Write-Output "This is result"
Write-Warning "This is warning""
This will generate error because this is not a ps command.
它将生成一个带有
results.txt
的文件
error.txt
,其中
results.txt
是警告和stdout/result的组合
warning.txt
仍然为空。我仍然不明白为什么PowerShell警告没有被过滤并正确重定向到单独的文件

error.txt:

this:术语“this”不被识别为cmdlet、函数、,
脚本文件或可操作程序。检查名称的拼写,或者检查路径是否正确
已包括,请验证路径是否正确,然后重试。
位于C:\Users\Dell\Desktop\output.ps1:3 char:1
+这将生成错误,因为这不是ps命令。
+ ~~~~
+CategoryInfo:ObjectNotFound:(this:String)[],CommandNotFoundException
+FullyQualifiedErrorId:CommandNotFoundException
results.txt:

这是结果
警告:这是警告

我使用的是Windows 10和PowerShell版本5.1.14393.1358。

在下面的示例中,真正的问题是,当您使用
PowerShell.exe
执行ps1文件verbose警告,以及调试流合并到STDOUT)。(--产生一个空白的
warning.txt

为了避免powershell.exe中的此限制,上面的示例中有一个使用额外中间ps1文件的变通方法。您可以使用两个powershell文件和一个bat文件,并为
错误/警告/结果
获取三个单独的文件。(
2>error.txt 3>warning.txt>results.txt

output.ps1
提供与以下内容相同的内容

Write-Output "This is result"
Write-Warning "This is warning""
This will generate error because this is not a ps command.
然后创建另一个powershell文件。(我将其称为
newout.ps1
)和
newout.ps1
可以运行
output.ps1
,内容如下

.\output.ps1 2>error.txt 3>warning.txt >results.txt
最后,您的bat文件可以以这种方式运行
newout.ps1
1而不是
output.ps1

PowerShell.exe -Command "& '%~dp0newout.ps1'"

这将为您提供三个单独的文件和所需的结果

如何处理
2>warning.txt
?powershell/cmd无法重定向警告?使用2>可以重定向的错误。。但3>将生成一个空白输出。据我所知,cmd只有一个错误和一个标准流。
PowerShell.exe -Command "& '%~dp0newout.ps1'"