【发布时间】:2021-12-29 06:41:29
【问题描述】:
我希望能够使用函数在 AzureCLI 任务中简化一些内联 powerscript,类似于以下内容:
- task: AzureCLI@2
displayName: "My Task"
inputs:
scriptType: pscore
scriptLocation: inlineScript
inlineScript: |
Do-Something "hello" "world"
Do-Something "goodbye" "world"
function Do-Something {
Param
(
[Parameter(Mandatory=$true, Position=0)]
[string] $Hello,
[Parameter(Mandatory=$true, Position=1)]
[string] $World
)
Write-Host "$Hello $World"
}
但是这会失败并出现以下错误:
+ Do-Something "hello" "world"
+ ~~~~~~~
+ CategoryInfo : ObjectNotFound: (Do-Something:String) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : CommandNotFoundException
##[error]Script failed with exit code: 1
这可能吗?如果是这样,我做错了什么?
【问题讨论】:
-
您需要先定义函数(iow.执行以
function关键字开头的语句),然后才能调用它。将Do-Something ...语句移到函数定义下方,它会起作用
标签: function powershell azure-pipelines-yaml hoisting