如何在 powershell 重定向中将警告消息与 stdout/result 分开?
How to separate warning message from stdout/result in powershell redirection?
我正在尝试在 PowerShell 中编写一个简单的测试脚本 separate warning message from stdout/result in order to get two separate text files, one for warnings and one for standard results in PowerShell Redirection。
这是我的简单 output.ps1
脚本:
Write-Output "This is result"
Write-Warning "This is warning"
我正在尝试 运行 output.ps1
使用以下命令使用 .bat 文件:
PowerShell.exe -Command "& '%~dp0output.ps1'" 3>warning.txt >results.txt
pause
但我的问题是 warning.txt
完全是空的,而警告和结果都写入了 results.txt
。
This is result
WARNING: This is warning
我在 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.
它将按预期生成错误 error.txt
文件 results.txt
,其中 results.txt
是警告和 stdout/result 的组合。 warning.txt
仍然是空的。我仍然不明白为什么没有过滤 PowerShell 警告并将其正确重定向到单独的文件。
error.txt:
this : The term 'this' is not recognized as the name of a cmdlet, function,
script file, or operable program. Check the spelling of the name, or if a path
was included, verify that the path is correct and try again.
At C:\Users\Dell\Desktop\output.ps1:3 char:1
+ This will generate error because this is not a ps command.
+ ~~~~
+ CategoryInfo : ObjectNotFound: (this:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
results.txt:
This is result
WARNING: This is warning
我正在使用 Windows 10 和 PowerShell 版本 5.1.14393.1358。
真正的问题,在下面的例子中,是,当你使用 powershell.exe
执行一个 ps1 文件时 verbose, warning ,和 debug 流合并到 STDOUT) -Source。 (---结果为空白warning.txt
)
PowerShell.exe -Command "& '%~dp0output.ps1'" 3>warning.txt >results.txt
为了规避 powershell.exe
中的这个限制,上面的示例有一个使用额外的中间 ps1 文件的解决方法。您可以使用两个 powershell 文件和一个 bat 文件,并为 error/warning/results
获取三个单独的文件。 (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'"
这会为您提供三个具有所需结果的独立文件。
我正在尝试在 PowerShell 中编写一个简单的测试脚本 separate warning message from stdout/result in order to get two separate text files, one for warnings and one for standard results in PowerShell Redirection。
这是我的简单 output.ps1
脚本:
Write-Output "This is result"
Write-Warning "This is warning"
我正在尝试 运行 output.ps1
使用以下命令使用 .bat 文件:
PowerShell.exe -Command "& '%~dp0output.ps1'" 3>warning.txt >results.txt
pause
但我的问题是 warning.txt
完全是空的,而警告和结果都写入了 results.txt
。
This is result
WARNING: This is warning
我在 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.
它将按预期生成错误 error.txt
文件 results.txt
,其中 results.txt
是警告和 stdout/result 的组合。 warning.txt
仍然是空的。我仍然不明白为什么没有过滤 PowerShell 警告并将其正确重定向到单独的文件。
error.txt:
this : The term 'this' is not recognized as the name of a cmdlet, function,
script file, or operable program. Check the spelling of the name, or if a path
was included, verify that the path is correct and try again.
At C:\Users\Dell\Desktop\output.ps1:3 char:1
+ This will generate error because this is not a ps command.
+ ~~~~
+ CategoryInfo : ObjectNotFound: (this:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
results.txt:
This is result
WARNING: This is warning
我正在使用 Windows 10 和 PowerShell 版本 5.1.14393.1358。
真正的问题,在下面的例子中,是,当你使用 powershell.exe
执行一个 ps1 文件时 verbose, warning ,和 debug 流合并到 STDOUT) -Source。 (---结果为空白warning.txt
)
PowerShell.exe -Command "& '%~dp0output.ps1'" 3>warning.txt >results.txt
为了规避 powershell.exe
中的这个限制,上面的示例有一个使用额外的中间 ps1 文件的解决方法。您可以使用两个 powershell 文件和一个 bat 文件,并为 error/warning/results
获取三个单独的文件。 (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'"
这会为您提供三个具有所需结果的独立文件。