Emi*_*ggi 17
Write-Host将对象发送给主机.它不会返回任何对象.
http://technet.microsoft.com/en-us/library/hh849877.aspx
这就是您无法将输出重定向到文件的原因.如果你有很多写主机语句,那么用Write-Host
你可以决定输出到控制台,文件或事件日志,还是全部三个.
检查还:
Write-Host
Write-Information
6>> file_name
Write-Host
.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)
使用重定向将导致Write-Host挂起.这是因为Write-Host处理特定于当前使用的终端的各种格式问题.如果您只希望脚本具有正常输出的灵活性(默认为shell,具有>,2>等功能),请使用Write-Output.
否则,如果您真的想要捕捉当前终端的特性,Start-Transcript是一个很好的起点.否则你将不得不手工测试或编写一些复杂的测试套件.
小智 7
*
尝试在尖括号前添加星号>
以重定向所有流:
powershell -文件 Your-Script.ps1
*
> 输出.log
当请求流重定向时,如果没有指定特定流,则默认情况下仅重定向Success Stream
( )。是写入( )的别名。要重定向所有流,请使用.1>
Write-Host
Write-Information
Information Stream
6>
*>
Powershell-7.1支持多个输出流的重定向:
Write-Output
Write-Error
Write-Warning
Write-Verbose
Write-Debug
Write-Information
小智 6
您可以在辅助PowerShell Shell中运行脚本并捕获如下输出:
powershell -File 'Your-Script.ps1' > output.log
Run Code Online (Sandbox Code Playgroud)
那对我有用。
归档时间: |
|
查看次数: |
66021 次 |
最近记录: |