我希望能夠使用函式簡化 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
這可能嗎?如果是這樣,我做錯了什么?
uj5u.com熱心網友回復:
Mathias R. Jessen提供了關鍵的指示:
Azure 的使用是您的問題的附帶問題,它源于基本的 PowerShell 行為:
與其它語言不同,PowerShell的執行沒有功能提升,...
......這意味著你必須宣告你的功能,第一之前,你可以給他們打電話,也就是說,在源代碼中,你必須將函式的定義之前呼叫它的任何宣告。
不幸的是,相關的概念幫助主題about_Functions 從歷史上看并沒有說清楚,但修復程式正在等待中(可能在您閱讀本文時已就位)。需要手動更新現有 PowerShell 安裝的脫機幫助才能查看更改。
為您的代碼拼出解決方案:
- task: AzureCLI@2
displayName: "My Task"
inputs:
scriptType: pscore
scriptLocation: inlineScript
inlineScript: |
# First, declare the function.
function Do-Something {
Param
(
[Parameter(Mandatory=$true, Position=0)]
[string] $Hello,
[Parameter(Mandatory=$true, Position=1)]
[string] $World
)
Write-Host "$Hello $World"
}
# Now you can call it.
Do-Something "hello" "world"
Do-Something "goodbye" "world"
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/362444.html
