如何从我的 Azure 函数引用其他 powershell 函数?

Col*_*hie 3 powershell azure azure-functions

如何在我的用 powershell 编写的 Azure 函数中的 run.ps1 文件中引用其他 powershell 函数?

细节:

我有 25 个私有的“助手”函数,我编写了这些函数来帮助操作数据、哈希表,并且通常使我可以更轻松地在 powershell 中编写脚本。我一直在更改和添加这些函数的功能,我更喜欢将它们组合在一个lib文件夹中,然后在我的 Azure Function 冷启动时导入。

我可以通过在我的 run.ps1 文件顶部包含我的“帮助程序”函数来完成所有这些工作(我不想这样做,因为这感觉很笨拙并且不允许我将每个函数分开到它自己的文件中)。

如何通过将我的所有函数分离到它们自己的文件中然后源/导入它们来使其工作?

我试过像这样设置我的文件夹结构:

FunctionApp
 | - host.json
 | - profile.ps1
 | - lib
 | | - helperfunction1.ps1
 | | - helperfunction2.ps1
 | | - helperfunction3.ps1
... (etc)
 | - myFunction
 | | - function.json
 | | - run.ps1
Run Code Online (Sandbox Code Playgroud)

我在我的 profile.ps1 文件中使用此代码导入每个函数:

$functionFiles = Get-ChildItem -Path "$PSScriptRoot\lib" -Filter *.ps1
Write-Information "Loading scripts"
foreach($file in $functionFiles){
    Write-Information "Sourcing $($file.FullName)"
    . $file.FullName
}
Run Code Online (Sandbox Code Playgroud)

当我在本地运行/测试时,一切正常,但是当我部署到 Azure 时,它​​会失败并显示以下堆栈跟踪:

2019-07-12T18:25:10.461 [Error] Executed 'Functions.HttpTriggerClientIssues' (Failed, Id=bf6fccdd-8972-48a0-9222-cf5b19cf2d8e)
Result: Failure
Exception: Value cannot be null.
Parameter name: value
Stack:    at Google.Protobuf.ProtoPreconditions.CheckNotNull[T](T value, String name)
   at Microsoft.Azure.WebJobs.Script.Grpc.Messages.StreamingMessage.set_RequestId(String value) in C:\projects\azure-functions-powershell-worker\src\Messaging\protobuf\FunctionRpc.cs:line 309
   at Microsoft.Azure.Functions.PowerShellWorker.Utility.RpcLogger.Log(Level logLevel, String message, Exception exception, Boolean isUserLog) in C:\projects\azure-functions-powershell-worker\src\Logging\RpcLogger.cs:line 46
   at Microsoft.Azure.Functions.PowerShellWorker.PowerShell.PowerShellManager.InvokeProfile(String profilePath) in C:\projects\azure-functions-powershell-worker\src\PowerShell\PowerShellManager.cs:line 181
   at Microsoft.Azure.Functions.PowerShellWorker.PowerShell.PowerShellManager.Initialize() in C:\projects\azure-functions-powershell-worker\src\PowerShell\PowerShellManager.cs:line 106
   at Microsoft.Azure.Functions.PowerShellWorker.PowerShell.PowerShellManagerPool.CheckoutIdleWorker(StreamingMessage request, AzFunctionInfo functionInfo) in C:\projects\azure-functions-powershell-worker\src\PowerShell\PowerShellManagerPool.cs:line 99
   at Microsoft.Azure.Functions.PowerShellWorker.RequestProcessor.ProcessInvocationRequest(StreamingMessage request) in C:\projects\azure-functions-powershell-worker\src\RequestProcessor.cs:line 235
Run Code Online (Sandbox Code Playgroud)

我很困惑为什么它可以在本地运行,但不能在 Azure 中运行。最后,我真的希望能够在自己的文件中定义我的所有函数,将它们导入一次,并能够在我的 run.ps1 文件中使用它们(甚至更好,如果我在其中创建了多个 HTTP 触发器)相同的 FunctionApp,我希望能够跨触发器共享我的辅助函数)

Col*_*hie 5

我想通了。

将您的辅助函数放在 Modules 文件夹内的 .psm1 文件中,如下所示:

FunctionApp
 | - host.json
 | - profile.ps1
 | - Modules
 | | - helperfunction1.psm1
 | | - helperfunction2.psm1
 | | - helperfunction3.psm1
... (etc)
 | - myFunction
 | | - function.json
 | | - run.ps1
Run Code Online (Sandbox Code Playgroud)

尽管文档目前说您的辅助函数将自动可用,但我的经验表明您仍然需要导入文件:

#in profile.ps1:
foreach($file in Get-ChildItem -Path "$PSScriptRoot\Modules" -Filter *.psm1){
    Import-Module $file.fullname
}
Run Code Online (Sandbox Code Playgroud)

这现在似乎对我有用。

谢谢~!