【问题标题】:How do I reference other powershell functions from my Azure Function?如何从我的 Azure 函数中引用其他 powershell 函数?
【发布时间】:2019-11-22 13:00:53
【问题描述】:

如何在我用 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

我在 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,我希望能够跨触发器共享我的辅助函数)

【问题讨论】:

  • 请提供详细的错误日志,如果可能,请提供代码。还尝试打印路径 Get-ChildItem -Path "$PSScriptRoot\lib" 并检查该路径下的文件是否可用。如果未启用日志记录,请添加信息日志记录,以便我们了解哪一行导致问题。
  • @MohitVerma-MSFT 我重新阅读了文档,我想知道“DOT 采购”我的功能是否甚至可以在 Azure 中工作。我按照docs.microsoft.com/en-us/azure/azure-functions/… 的说明创建了一个“模块”文件夹并将 .psm1 文件放在那里,但它仍然无法使 .psm1 文件中定义的功能可用:Result: ERROR: The term 'get-cctestfunction3' is not recognized as the name of a cmdlet, function, script file, or operable program. 所以要么文档有误,要么我'我做得不对:-)
  • 我并没有假设我的 .psm1 文件会自动导入,而是尝试在我的 run.ps1 中显式导入它们:Import-Module 'D:\home\site\wwwroot\Modules\get-cctestfunction3.psm1' $mystring = get-cctestfunction3 $body += "mystring: $mystring<br/>" 那行得通。但我不确定是否要显式导入 run.ps1 中的所有 .psm1 文件?

标签: azure powershell azure-functions


【解决方案1】:

我明白了。

将您的辅助函数放在 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
}

这似乎对我有用。

谢谢~!

【讨论】:

  • 感谢分享。我做了一些测试,为了让 AzFunction 自动拾取模块,它需要有一个模块清单 (psd1),并且您的模块必须列在“requirements.psd1”中。
猜你喜欢
  • 2021-01-30
  • 1970-01-01
  • 2013-08-15
  • 1970-01-01
  • 2020-09-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多