下面是我PowerShell的环境(通过get-host命令):PowerShell写入输出在try/catch块中不起作用?
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更改为无效,以查看是否将错误消息写入输出文件 - 这是我感到困惑的地方。看来写日志的唯一方法是使用写主机。当我使用写入输出时,异常消息从不写入日志文件。下面是我使用的代码:
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,然而,似乎写主机是我可以在输出文件中得到错误信息的唯一途径。
我的问题是:如何编写一个函数,使得REST请求成功返回结果,但如果发生错误,请将错误消息写入输出文件并返回$ null?
因为我对PowerShell相当绿色,任何意见将不胜感激。
“*#如果使用写输出 - 没有写入输出文件???#必须使用写主机将错误信息包含在输出文件中 - 为什么?*” - 这显然不是真的,我把你之前提出过的问题展示出来的代码。你说过你使用'* >>'重定向所有输出流,而写主机是流6并且将被重定向;在这个版本中你正在使用'| out-file“,它只捕获流水线输出,不会。并且流水线输出不会出现,因为您在'$ json'中捕获它,然后对'$ json'执行任何操作。 – TessellatingHeckler
正确 - 但我删除了,然后转发问题以更好地说明发生了什么 - 写输出从未被写入日志文件 - 所以我重写了代码并编写了一个log_write函数 - 一切正常现在应该;感谢你花时间发布回复;总是通过阅读答复来学习一些东西! ;) – bdcoder