【发布时间】:2017-09-17 14:03:02
【问题描述】:
下面是我的 Powershell 环境(通过 Get-Host 命令):
Name : Windows PowerShell ISE Host
Version : 5.1.15063.608
InstanceId : 46c51405-fc6d-4e36-a2ae-09dbd4069710
UI : System.Management.Automation.Internal.Host.InternalHostUserInterface
CurrentCulture : en-US
CurrentUICulture : en-US
PrivateData : Microsoft.PowerShell.Host.ISE.ISEOptions
DebuggerEnabled : True
IsRunspacePushed : False
Runspace : System.Management.Automation.Runspaces.LocalRunspace
我有一个向资源发出 REST 请求的脚本——一切正常。
在测试时,我将 URL 更改为无效,以查看是否将错误消息写入输出文件——这是我感到困惑的地方。似乎将异常写入日志的唯一方法是使用 Write-Host。当我使用 Write-Output 时,异常消息永远不会写入日志文件。以下是我正在使用的代码:
function rest_request( $method, $uri )
{
# Code here create @req dictionary
try {
$json = Invoke-RestMethod @req
} catch {
# If using Write-Output - Nothing is written to output file???
# MUST use Write-Host for error info to be included in output file - WHY?
Write-Host "* REST Request Error:"
Write-Host $error[0] | Format-List -force
$json = $null
}
return $json
}
function do_something()
{
Write-Output "Start..."
# other functions called here but removed for clarity.
# The other functions contain Write-Output statements
# which are showing in the log file.
# Issue REST request with invalid URL to cause exception ...
$json = rest_request "Get" "https://nowhere.com/rest/api"
Write-Output "Done."
}
do_something | Out-File "D:\temp\log_file.txt" -Append
根据这篇博文:using Write-Host should be avoided,但似乎 Write-Host 是我在输出文件中获取错误消息的唯一方法。
我的问题是:如何编写一个函数来发出 REST 请求并在成功时返回结果,但如果发生错误,将错误消息写入输出文件并返回 $null?
由于我对 PowerShell 相当陌生,因此我们将不胜感激。
【问题讨论】:
-
"# 如果使用 Write-Output - 没有任何内容写入输出文件??? # 必须使用 Write-Host 以将错误信息包含在输出文件中 - 为什么? “ - 这显然是不正确的,我输入了上次你问同样问题时演示的代码。你说你当时使用
*>>重定向所有输出流,写主机是流6,将被重定向;在这个版本中,您使用的是| out-file,它只捕获管道输出,它不会。并且管道输出不会输出文件,因为您在$json中捕获它,然后对$json不执行任何操作。 -
是的 - 但我删除然后重新发布问题以更好地说明正在发生的事情 - 写入输出从未写入日志文件 - 所以我重新编写代码并编写了 log_write功能——一切正常;感谢您抽出时间回复;总是通过阅读回复来学习一些东西! ;)
标签: powershell