小编lab*_*uer的帖子

PowerShell stderr 重定向到文件插入换行符

编辑:我为(反对?)这种行为创建了一个 PowerShell UserVoice “建议”;随意点赞。

当我重定向到文件时,PowerShell(5.1.16299.98,Windows 10 Pro 10.0.16299)正在将换行符插入到我的 stderr 中 - 就像格式化控制台一样。让我们生成任意长度的错误消息:

class Program
{
    static void Main(string[] args)
    {
        System.Console.Error.WriteLine(new string('x', int.Parse(args[0])));
    }
}
Run Code Online (Sandbox Code Playgroud)

我将上述内容编译为longerr.exe. 然后我这样称呼它:

$ .\longerr.exe 60 2>error.txt
Run Code Online (Sandbox Code Playgroud)

我在窗口宽度为 60 的 PowerShell 控制台中运行了以下脚本:

$h = '.\longerr.exe : '.Length
$w = 60 - 1
$f = 'error.txt'
Remove-Item $f -ea Ignore
(($w-$h), ($w-$h+1), ($w), ($w+1), ($w*2-$h), ($w*2-$h+1)) |
    % { 
        $_ >> $f
        .\longerr.exe $_ 2>>$f
    }
Run Code Online (Sandbox Code Playgroud)

现在在更广泛的控制台中,我运行了以下命令:

$ Get-Content $f | Select-String '^(?![+\t]|At line| …
Run Code Online (Sandbox Code Playgroud)

powershell stderr io-redirection

5
推荐指数
1
解决办法
759
查看次数

标签 统计

io-redirection ×1

powershell ×1

stderr ×1