带-noexit的Powershell不会将错误写入stderr

带-noexit的Powershell不会将错误写入stderr,powershell,Powershell,如果我有导致错误的powershell脚本,那么如果我使用-noexit,就无法将错误写入stderr 例如,如果我有以下脚本foo.ps1 Get-Process none 如果在不使用-noexit的情况下运行此命令,则错误将写入stderr PS E:\Play>powershell -file .\foo.ps1 2>Error1.txt PS E:\Play>powershell -noexit -file .\foo.ps1 2>Error2.txt 如

如果我有导致错误的powershell脚本,那么如果我使用-noexit,就无法将错误写入stderr

例如,如果我有以下脚本foo.ps1

Get-Process none
如果在不使用-noexit的情况下运行此命令,则错误将写入stderr

PS E:\Play>powershell -file .\foo.ps1 2>Error1.txt
PS E:\Play>powershell -noexit -file .\foo.ps1 2>Error2.txt
如果使用-noexit参数,则不会将错误写入stderr

PS E:\Play>powershell -file .\foo.ps1 2>Error1.txt
PS E:\Play>powershell -noexit -file .\foo.ps1 2>Error2.txt

使用-noexit时,我必须做什么才能将错误写入stderr?

为什么要从powershell提示符运行
powershell
?只用

PS E:\Play>.\foo.ps1 2>Error1.txt
或(见)

要从
cmd.exe
命令提示符运行上述语句,需要在powershell中保留一些字符(请注意所有
^
插入符号):


省略
-file
开关并在命令提示符下使用例如
-NoExit
,请参见
powershell/?

您的用例是什么?我有一个脚本,它使用FileSystemWatcher监视正在创建的文件,因此我想用-NoExit启动它。我想将stderr捕获到一个文件中,但无法做到这一点。由于我无法实现这一点,我使用了Try/Catch,它允许be捕获所有异常并适当地记录它们
E:\Play>powershell .\foo.ps1 *^>^&1 ^| Tee-Object -FilePath Error1.txt