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

标签 azure powershell azure-functions

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

详细信息:

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

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

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

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

FunctionApp
 | - host.json
 | - profile.ps1
 | - lib
 | | - helperfunction1.ps1
 | | - helperfunction2.ps1
 | | - helperfunction3.ps1
... (etc)
 | - myFunction
 | | - function.json
 | | - run.ps1

我在 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
}

当我在本地运行/测试时,一切都很好,但是当我部署到 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

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

最佳答案

我明白了。

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

FunctionApp
 | - host.json
 | - profile.ps1
 | - Modules
 | | - helperfunction1.psm1
 | | - helperfunction2.psm1
 | | - helperfunction3.psm1
... (etc)
 | - myFunction
 | | - function.json
 | | - run.ps1

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

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

目前看来这对我有用。

谢谢~!

关于azure - 如何从我的 Azure Function 引用其他 powershell 函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57012401/

相关文章:

powershell - 循环直到用户输入三个必需字母之一

sql-server - 在存储过程中编码的 SQLCMD、PowerShell、BCP 工具 - SQL Server 托管实例

azure - 获取 AzRoleAssignment : Scope '/subscriptions' should have even number of parts

azure - 如何在 Windows Azure 上使用 NTP 验证本地 VM 时钟?

c# - Azure网站不断抛出错误 "An attempt was made to access a socket in a way forbidden by its access permissions"

c# - 如何将Azure 2.3项目升级到Azure 2.4?

visual-studio - Visual Studio 2017-无法初始化Powershell主机

c# - 无法在 Azure Functions 中加载文件或程序集 'Newtonsoft.Json, Version=10.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed'

c# - 如何获取Azure中所有存储帐户的列表? (C#)

azure - 如何在团队 channel 传入 Webhook 中提及用户?