python - 将错误从 Python 返回到 PowerShell?
问题描述
我在 PowerShell 中运行多个 Python 脚本,并且我希望在 Python 脚本中的特定条件下通知 PowerShell 主脚本。就像从 Python 中抛出错误并使用 PowerShell 捕获它一样。
Python 示例:
import os
print "Running Python script ..."
raise Exception("This Error is coming from Python")
和 PowerShell 主脚本:
$ErrorActionPreference = 'Stop'
try {
python runThisScript.py -ErrorAction Stop
# Write-Error -Message 'Oh no! An error!'
Write-Host "What is that?"
} catch {
Write-Host $_.Exception.Message -ForegroundColor Green
throw [System.Exception] "Python scrip XXX failed"
} finally {
Write-Host "This is the final block ..."
}
但这不起作用。它将显示:
Running Python script ...
Traceback (most recent call last):
File "runThisScript.py", line 5, in <module>
raise Exception("This Error is coming from Python")
Exception: This Error is coming from Python
What is that?
This is the final block ...
如果我取消注释该行,Write-Error -Message 'Oh no! An error!'
这就是我想要看到的(错误是由 PowerShell 捕获和处理的)。
Oh no! An error!
This is the final block ...
Python scrip XXX failed
At C:\Automation\general_stuff\tc\mainTestRunner.ps1:12 char:5
+ throw [System.Exception] "Python scrip XXX failed"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (:) [], Exception
+ FullyQualifiedErrorId : Python scrip XXX failed
预先感谢您的意见。
解决方案
推荐阅读
- python-3.x - 在 python (globals())[method](param1,param2) 中与 exec('method(param1,param2)?
- r - 我需要帮助找到使用 R 工作室的间歇泉的短喷发团和长喷发团
- java - @Mapper 导入语句“import org.apache.ibatis.annotations.Mapper;”出现错误
- jquery - 检测用户何时在移动手指时按住特定 div
- packet - 使用 ML 检测 DDoS 后如何在 ryu 控制器中丢弃数据包
- c# - 为什么要在 AuthenticationBuilder.AddScheme 中配置选项
不工作? - botframework - 从 1:1 聊天机器人消息中单击 Teams 客户端中的通知时如何修复焦点中断?- 焦点转到活动选项卡而不是聊天
- ios - 从水平更改为垂直时如何更改堆栈视图的排序逻辑
- http - iframe 中的页面没有看到自己的 cookie
- dialogflow-es - Dialogflow -> Actions Builder 迁移