powershell 2.0 redirection file handle exception

前端 未结 1 1060
孤街浪徒
孤街浪徒 2021-01-02 15:11

I\'m looking for a solution to the The OS handle\'s position is not what FileStream expected. Do not use a handle simultaneously in one FileStream and in Win32 code or

相关标签:
1条回答
  • 2021-01-02 15:47

    If you do this in foo.ps1 it solves the problem:

    # <fix>
    $bindingFlags = [Reflection.BindingFlags] "Instance,NonPublic,GetField"
    $objectRef = $host.GetType().GetField( "externalHostRef", $bindingFlags ).GetValue( $host )
    $bindingFlags = [Reflection.BindingFlags] "Instance,NonPublic,GetProperty"
    $consoleHost = $objectRef.GetType().GetProperty( "Value", $bindingFlags ).GetValue(     $objectRef, @() )
    [void] $consoleHost.GetType().GetProperty( "IsStandardOutputRedirected", $bindingFlags).GetValue( $consoleHost, @() )
    $bindingFlags = [Reflection.BindingFlags] "Instance,NonPublic,GetField"
    $field = $consoleHost.GetType().GetField( "standardOutputWriter", $bindingFlags )
    $field.SetValue( $consoleHost, [Console]::Out )
    $field2 = $consoleHost.GetType().GetField( "standardErrorWriter", $bindingFlags )
    $field2.SetValue( $consoleHost, [Console]::Out )
    # </fix>
    
    write-host "normal"
    write-error "error"
    write-host "yay"
    
    powershell .\bar.ps1 2>&1 | more
    

    Piping the output through more hides the fact that it is ultimately going to a file from the child instance of Powershell, bypassing the bug.

    In fact, if you create a grandparent script foobar.ps1 which just runs foo.ps1:

    powershell .\foo.ps1 2>&1 | more
    

    Then you don't need "the fix" at all, and foo.ps1 can just be

    write-host "normal"
    write-error "error"
    write-host "yay"
    
    .\bar.ps1
    

    because the piping solves the problem for all descendent scripts.

    0 讨论(0)
提交回复
热议问题