[英]PowerShell Param() statement + error trap
We're using the following statement to enable TeamCity to recognize errors in our PowerShell deployment scripts: 我们使用以下语句使TeamCity能够识别PowerShell部署脚本中的错误:
trap { $host.SetShouldExit(1) }
This works fine, however, since Param(...) needs to be the very first statement we have to use this order: 但是,这很好用,因为Param(...)必须是第一个必须使用此顺序的语句:
Param(
...
)
Set-StrictMode -Version 2.0
$ErrorActionPreference = "Stop"
trap { $host.SetShouldExit(1) }
Is there any way to also trap errors during the Param() evaluation? 有没有办法在Param()评估期间也捕获错误? Eg if we omit a mandatory parameter, TeamCity is not able to detect this at the moment.
例如,如果我们省略一个强制性参数,TeamCity目前无法检测到该参数。
This is yet another annoying oversight of Powershell. 这是对Powershell的又一个烦人的监督。 I find the best way to workaround this Powershell bug is to roll your own mandatory verification.
我发现解决此Powershell错误的最佳方法是推出自己的强制性验证。 It's more code, but clear, simple and fail-safe.
它是更多的代码,但清晰,简单且具有故障保护功能。
First remove the mandatory flag from the param declaration and check for null or empty at the beginning of the script. 首先,从参数声明中删除必需标志,并在脚本开头检查是否为null或为空。
param (
[string] $required_param
)
# Check required params
if ([string]::IsNullOrEmpty($required_param)) {
Write-Host "The required_param is required."
exit 1;
}
Plus you can call your script with the regular File argument: 另外,您可以使用常规File参数调用脚本:
powershell -File foo.ps1
Hope this helps. 希望这可以帮助。
Try putting the trap in the TeamCity string to execute. 尝试将陷阱放入TeamCity字符串中以执行。 For instance, say I have a script with a param block at the top of the script (foo.ps1) with a mandatory parameter that I don't supply eg:
例如,假设我有一个脚本,该脚本的脚本顶部带有一个param块(foo.ps1),带有一个我不提供的强制性参数,例如:
param([Parameter(Mandatory)][int]$num, [bool]$bool)
Then I can execute it like so from CMD and get an error exit code: 然后,我可以从CMD像这样执行它,并获取错误退出代码:
C:\> cmd /c Powershell.exe -NonInteractive -Command "& {trap {exit 1}; c:\foo.ps1}"
C:\> %ERRORLEVEL%
1
BTW you might find this blog post an interesting read. 顺便说一句,您可能会发现此博客文章有趣。
If I understand you correctly, you want to execute a PowerShell script, and have TeamCity see the error returned. 如果我理解正确,则您想执行PowerShell脚本,并让TeamCity查看返回的错误。 I think that this is very similar to how I catch and return errors in my scripts that need to be run with scheduled tasks.
我认为这与我需要在计划任务中运行的脚本中捕获并返回错误的方式非常相似。
How I do it is first make functions/cmdlts for everything you want to do. 我要做的是首先为要执行的所有操作创建功能/ cmdlts。 Then execute those functions in a try..catch block.
然后在try..catch块中执行这些功能。 eg
例如
#My test function
Function test{
Param( [Parameter(Mandatory=$true)] [int]$a )
Process
{
Write-Host "Hi $a"
}
}
#Execute Function
Try{
test 1234 -ErrorAction "Stop"
}
Catch
{
#Error encountered
Write-Host "Error Encountered: exiting"
#Return Bad error code
Exit 1
}
This should catch any parameters that are incorrect. 这应该捕获任何不正确的参数。
This may not be applicable, but other people's reference, for scheduled tasks, I run my scheduled tasks with the following command: 这可能不适用,但是对于其他人的参考,对于计划任务,我使用以下命令运行计划任务:
powershell.exe -Command ". C:\Scripts\Run_PS1.ps1 ; exit $LASTEXITCODE"
This will run powershell, execute the script, and then correctly return the last exit code back to the scheduled task. 这将运行powershell,执行脚本,然后将最后一个退出代码正确地返回计划任务。
Hope this helps you out. 希望这可以帮助你。
Actually I had the same question and ended up on combining both approaches: 实际上,我有同样的问题,最终将两种方法结合在一起:
-Command
calling to powershell.exe
to allow to trap the Parameter error -Command
调用powershell.exe
以允许捕获参数错误 trap
within the Powershell script to handle errors within - and use $LASTEXITCODE
to counter balance Powershell's behavior trap
处理内部错误-并使用$LASTEXITCODE
抵消Powershell的行为 Calling side in BAT
/ CMD
: BAT
/ CMD
呼叫方:
@echo off
powershell -NoLogo -NonInteractive -Command "& {trap {Write-Error $_; exit 2}; scipt.ps1 %1 %2 %3 %4 %5 %6 %7 %8 %9 ; exit $LASTEXITCODE}"
IF %ERRORLEVEL% NEQ 0 Exit /B %ERRORLEVEL%
Goto :EOF
:EOF
The PowerShell script PowerShell脚本
Param(
...
)
Set-StrictMode -Version 2.0
$ErrorActionPreference = "Stop"
trap { Exit(1) }
...
That way one can decide between Param
errors (exit code 2
) and errors in the script itself (exit code 1
) 这样一来,您就可以在
Param
错误(退出代码2
)和脚本本身的错误(退出代码1
)之间做出决定。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.