如何从Azure函数中引用其他powershell函数?
如何在powershell中编写的Azure函数中引用run.ps1文件中的其他powershell函数 详细信息: 我有25个私有的“helper”函数,我已经编写了这些函数来帮助操作数据、哈希表,并使我在powershell中编写脚本变得更容易。我一直在不断地更改和添加这些函数的功能,我更希望将它们组合到如何从Azure函数中引用其他powershell函数?,azure,powershell,azure-functions,Azure,Powershell,Azure Functions,如何在powershell中编写的Azure函数中引用run.ps1文件中的其他powershell函数 详细信息: 我有25个私有的“helper”函数,我已经编写了这些函数来帮助操作数据、哈希表,并使我在powershell中编写脚本变得更容易。我一直在不断地更改和添加这些函数的功能,我更希望将它们组合到lib文件夹中,然后在Azure函数冷启动时导入 我可以通过在run.ps1文件的顶部包含我的“helper”函数来实现所有这些功能(我不想这样做,因为这让我觉得很麻烦,也不允许我将每个函数
lib
文件夹中,然后在Azure函数冷启动时导入
我可以通过在run.ps1文件的顶部包含我的“helper”函数来实现所有这些功能(我不想这样做,因为这让我觉得很麻烦,也不允许我将每个函数分离到它自己的文件中)
如何将所有函数分离到它们自己的文件中,然后对它们进行源代码/导入,从而实现这一点
我尝试过如下设置文件夹结构:
FunctionApp
| - host.json
| - profile.ps1
| - lib
| | - helperfunction1.ps1
| | - helperfunction2.ps1
| | - helperfunction3.ps1
... (etc)
| - myFunction
| | - function.json
| | - run.ps1
FunctionApp
| - host.json
| - profile.ps1
| - Modules
| | - helperfunction1.psm1
| | - helperfunction2.psm1
| | - helperfunction3.psm1
... (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时,它会因以下stacktrace而失败:
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文件中使用它们(甚至更好,如果我在同一个FunctionApp中创建了多个HTTP触发器,我希望能够跨触发器共享我的助手函数)我明白了
将助手函数放在Modules文件夹中的.psm1文件中,如下所示:
FunctionApp
| - host.json
| - profile.ps1
| - lib
| | - helperfunction1.ps1
| | - helperfunction2.ps1
| | - helperfunction3.ps1
... (etc)
| - myFunction
| | - function.json
| | - run.ps1
FunctionApp
| - host.json
| - profile.ps1
| - Modules
| | - helperfunction1.psm1
| | - helperfunction2.psm1
| | - helperfunction3.psm1
... (etc)
| - myFunction
| | - function.json
| | - run.ps1
尽管当前声明您的助手功能将自动可用,但根据我的经验,您仍然需要导入文件:
#in profile.ps1:
foreach($file in Get-ChildItem -Path "$PSScriptRoot\Modules" -Filter *.psm1){
Import-Module $file.fullname
}
这似乎对我现在起作用了
谢谢~ 请提供详细的错误日志,如果可能,请提供代码。另外,请尝试打印路径Get ChildItem-path“$PSScriptRoot\lib”,并检查该路径下的文件是否可用。如果未启用日志记录,请添加信息日志记录,以便我们能够了解是哪一行导致了问题。@MohitVerma MSFT我重新阅读了文档,我想知道我的“点源”功能是否可以在Azure中工作。我按照处的说明创建了一个“Modules”文件夹并将.psm1文件放在其中,但它仍然无法使.psm1文件中定义的函数可用:
Result:ERROR:术语“get-cctestfunction3”无法识别为cmdlet、函数、脚本文件的名称,或者可操作的程序。
因此,要么文档错误,要么我做得不对:-)我没有假设我的.psm1文件将自动导入,而是尝试在运行时显式导入它们。ps1:import Module'D:\home\site\wwroot\Modules\get-cctestfunction3.psm1'$mystring=get-cctestfunction3$body+=“mystring:$mystring
”
有效。但我不确定是否要显式导入run.ps1中的所有.psm1文件?