How do I reference other powershell functions from within my run.ps1 file in my Azure Function written in powershell?
Details:
I have 25 private, "helper" functions that I've written the help manipulate data, hashtables, and generally make it easier for me to write scripts in powershell. I'm constantly changing and adding capabilities to these functions and I'd prefer to have them grouped together in a lib folder then imported when my Azure Function cold-starts.
I can get all of this working by including my "helper" functions in the top of my run.ps1 file (which I don't want to do because this feels kludgy and doesn't allow me to separate each function into it's own file).
How do I get this to work by separating out all my functions into their own files and then source/importing them?
I've tried setting my folder structure like this:
FunctionApp
| - host.json
| - profile.ps1
| - lib
| | - helperfunction1.ps1
| | - helperfunction2.ps1
| | - helperfunction3.ps1
... (etc)
| - myFunction
| | - function.json
| | - run.ps1
And I use this code in my profile.ps1 file to import each function:
$functionFiles = Get-ChildItem -Path "$PSScriptRoot\lib" -Filter *.ps1
Write-Information "Loading scripts"
foreach($file in $functionFiles){
Write-Information "Sourcing $($file.FullName)"
. $file.FullName
}
Everything works great when I run/test locally, but when I deploy to Azure it fails with this 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
I'm confused why it works locally but not in Azure. In the end I'd really like to be able to define all of my functions in their own files, import them once, and be able to use them in my run.ps1 file (and even better, if I created multiple HTTP triggers within the same FunctionApp, I'd love to be able to share my helper functions across triggers)
Result: ERROR: The term 'get-cctestfunction3' is not recognized as the name of a cmdlet, function, script file, or operable program.So either the docs are wrong or I'm not doing it right :-)Import-Module 'D:\home\site\wwwroot\Modules\get-cctestfunction3.psm1' $mystring = get-cctestfunction3 $body += "mystring: $mystring<br/>"That worked. But I'm not sure I want to explicitly import all the .psm1 files in my run.ps1?