将Write-Host语句重定向到文件

Chi*_*ago 17 powershell

我有一个PowerShell脚本,我正在调试,并希望将所有写主机语句重定向到一个文件.有一个简单的方法吗?

Emi*_*ggi 17

Write-Host将对象发送给主机.它不会返回任何对象.

http://technet.microsoft.com/en-us/library/hh849877.aspx

这就是您无法将输出重定向到文件的原因.如果你有很多写主机语句,那么用Write-Host你可以决定输出到控制台,文件或事件日志,还是全部三个.

检查还:

  • 我不认为这是一个答案.在Powershell 3.0中,如果我尝试`write-host"test"> c:\ temp\out.txt`我在文件中没有得到任何东西.输出将写入控制台 (2认同)
  • 是的,但答案说,如果我不想替换我应该"尝试追加重定向"的写主机语句 - 这是我在我的答案中尝试过的,哪个失败了 (2认同)

lat*_*kin 14

您可以创建一个代理函数-Quiet,将对象发送到标准输出流,而不是仅打印它们.我为此目的编写了下面的cmdlet.它将动态创建代理,仅持续当前管道的持续时间.

我在博客上写完整的文章,但我已经包含了下面的代码.使用-Quiet开关来禁止控制台写入.

用法:

PS> .\SomeScriptWithWriteHost.ps1 | Select-WriteHost | out-file .\data.log  # Pipeline usage
PS> Select-WriteHost { .\SomeScriptWithWriteHost.ps1 } | out-file .\data.log  # Scriptblock usage (safer)

function Select-WriteHost
{
   [CmdletBinding(DefaultParameterSetName = 'FromPipeline')]
   param(
     [Parameter(ValueFromPipeline = $true, ParameterSetName = 'FromPipeline')]
     [object] $InputObject,

     [Parameter(Mandatory = $true, ParameterSetName = 'FromScriptblock', Position = 0)]
     [ScriptBlock] $ScriptBlock,

     [switch] $Quiet
   )

   begin
   {
     function Cleanup
     {
       # Clear out our proxy version of write-host
       remove-item function:\write-host -ea 0
     }

     function ReplaceWriteHost([switch] $Quiet, [string] $Scope)
     {
         # Create a proxy for write-host
         $metaData = New-Object System.Management.Automation.CommandMetaData (Get-Command 'Microsoft.PowerShell.Utility\Write-Host')
         $proxy = [System.Management.Automation.ProxyCommand]::create($metaData)

         # Change its behavior
         $content = if($quiet)
                    {
                       # In quiet mode, whack the entire function body,
                       # simply pass input directly to the pipeline
                       $proxy -replace '(?s)\bbegin\b.+', '$Object'
                    }
                    else
                    {
                       # In noisy mode, pass input to the pipeline, but allow
                       # real Write-Host to process as well
                       $proxy -replace '(\$steppablePipeline\.Process)', '$Object; $1'
                    }

         # Load our version into the specified scope
         Invoke-Expression "function ${scope}:Write-Host { $content }"
     }

     Cleanup

     # If we are running at the end of a pipeline, we need
     #    to immediately inject our version into global
     #    scope, so that everybody else in the pipeline
     #    uses it. This works great, but it is dangerous
     #    if we don't clean up properly.
     if($pscmdlet.ParameterSetName -eq 'FromPipeline')
     {
        ReplaceWriteHost -Quiet:$quiet -Scope 'global'
     }
   }

   process
   {
      # If a scriptblock was passed to us, then we can declare
      #   our version as local scope and let the runtime take
      #   it out of scope for us. It is much safer, but it
      #   won't work in the pipeline scenario.
      #
      #   The scriptblock will inherit our version automatically
      #   as it's in a child scope.
      if($pscmdlet.ParameterSetName -eq 'FromScriptBlock')
      {
        . ReplaceWriteHost -Quiet:$quiet -Scope 'local'
        & $scriptblock
      }
      else
      {
         # In a pipeline scenario, just pass input along
         $InputObject
      }
   }

   end
   {
      Cleanup
   }
}
Run Code Online (Sandbox Code Playgroud)


lab*_*nth 7

使用重定向将导致Write-Host挂起.这是因为Write-Host处理特定于当前使用的终端的各种格式问题.如果您只希望脚本具有正常输出的灵活性(默认为shell,具有>,2>等功能),请使用Write-Output.

否则,如果您真的想要捕捉当前终端的特性,Start-Transcript是一个很好的起点.否则你将不得不手工测试或编写一些复杂的测试套件.


小智 7

*尝试在尖括号前添加星号>以重定向所有流:

powershell -文件 Your-Script.ps1 *> 输出.log

当请求流重定向时,如果没有指定特定流,则默认情况下仅重定向Success Stream( )。是写入( )的别名。要重定向所有流,请使用.1>Write-HostWrite-InformationInformation Stream6>*>

Powershell-7.1支持多个输出流的重定向:

  • 成功流 (#1):PowerShell 2.0 Write-Output
  • 错误流 (#2):PowerShell 2.0Write-Error
  • 警告流 (#3):PowerShell 3.0 Write-Warning
  • 详细流 (#4):PowerShell 3.0 Write-Verbose
  • 调试流 (#5):PowerShell 3.0Write-Debug
  • 信息流 (#6):PowerShell 5.0 Write-Information
  • 所有流 (*):PowerShell 3.0


小智 6

您可以在辅助PowerShell Shell中运行脚本并捕获如下输出:

powershell -File 'Your-Script.ps1' > output.log
Run Code Online (Sandbox Code Playgroud)

那对我有用。