简体   繁体   English

从 Python 获取 Powershell 脚本的返回码

[英]Getting a return code from Python for Powershell script

EDITED to clean up the info:编辑清理信息:

I am writing a PowerShell script that will call another script.我正在编写一个 PowerShell 脚本,它将调用另一个脚本。

Once I get the return code, I want to use it in an if statement:一旦我得到返回码,我想在if语句中使用它:

MY CODE:我的代码:

if ($LASTEXITCODE -eq $TRUE) {
# Start job.
} else {
# Send failed email
}

EDIT 2: Correct format as noted by: Ansgar Wiechers编辑 2:正确的格式,如:Ansgar Wiechers

**if ($LASTEXITCODE -eq 0)** {
# Start job.
} else {
# Send failed email
}

As @AnsgarWiechers mentioned, you should be explicitly testing for $LASTEXITCODE -eq 0 instead of $true .正如@AnsgarWiechers 提到的,您应该明确测试$LASTEXITCODE -eq 0而不是$true

Here's a simple snippet for your example:这是您示例的简单代码段:

test.py content: test.py内容:

import sys
print(f'Testing for {sys.argv}')
assert sys.argv[1] == 'Success'

test.ps1 content: test.ps1内容:

py test.py Fail # or Success
if ($LASTEXITCODE -eq 0) {
    Write-Output "Python script ran successfully"
}
else {
    Write-Output "Python script failed"
}

Results:结果:

Testing for ['test.py', 'Success']
Python script ran successfully

# ...

Testing for ['test.py', 'Fail']
Traceback (most recent call last):
  File "test.py", line 5, in <module>
    assert sys.argv[1] == 'Success'
AssertionError
Python script failed

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM