我要在相同 PowerShell脚本中通过正则表达式找到ExitCode = 1或更高版本时设置终止步骤。
我将其上传到jenkins作业中,其中正在运行其他第三方软件,这些软件会返回Lastexitcode,而Powershell无法识别它们,因此我需要一个带有regex的函数来查找到目前为止编写的内容并在找到后终止。
我尝试过:
[regex]::new('ExitCode=[1-9]*[0-9]')
function script:ThrowErrors {
if ([regex]::new('ExitCode=[1-9]*[0-9]') {
'erorrrrr'
}
if ($env:Terminating -eq 1) {
throw 'This error is terminating the step.'
}
}
# ps1 log with steps and written output
# step one
1+2
'ExitCode=1'
'ExitCode=0'
'ExitCode=10'
$env:Terminating = '1'
ThrowErrors
# step two
2+3
我希望在运行该函数时,它将在整个脚本中找到指示的regex公式并终止该步骤,但是我到达了第二步,而该错误是它一开始什么都没找到。 / p>