skip to Main Content

I am trying to access files in the same function folder in a function app time trigger. I am using Powershell code.

How do I access the file

In an HTTP trigger I was able to access the script in the same folder by doing this

$script_path = ".$($TriggerMetadata.FunctionName)scriptfile.ps1"

However, this does not work if I am using a time trigger function as the $TriggerMetadata is not being passed when the function is triggered.

I want a way to be able to get the function name of the time trigger dynamically so I can access the script in the same folder.

How can I do this?

2

Answers


  1. Try this:

    # Get the current directory path
    $currentPath = Split-Path -Parent -Path $MyInvocation.MyCommand.Definition
    
    # Construct the file path for your script
    $scriptPath = Join-Path -Path $currentPath -ChildPath "scriptfile.ps1"
    
    # Use the $scriptPath to access the file
    
    Login or Signup to reply.
  2. If you are using Functions V2, You can get the function directory via $PSScriptRoot variable which is documented here.

    Function context information is
    exposed by the $TriggerMetadata variable in the run.ps1 file. This
    variable contains the function name, directory, and invocation id. If
    you are using a template that does not have the $TriggerMetadata
    variable, you can get function directory via $PSScriptRoot
    . See
    example below:

    using namespace System.Net
    
    # Input bindings are passed in via param block.
    param($Request, $TriggerMetadata)
    
    # Write to the Azure Functions log stream.
    Write-Host "PowerShell HTTP trigger function processed a request."
    
    write-output "Current directory:"
    write-output "PSScriptRoot: $PSScriptRoot"
    write-output "Directory: $($TriggerMetadata.FunctionDirectory)"
    
    write-output "FunctionName: $($TriggerMetadata.FunctionName)"
    write-output "InvocationId: $($TriggerMetadata.InvocationId)"
    
    # more code ...
    

    For Functions V1, You can use the EXECUTION_CONTEXT_FUNCTIONDIRECTORY environment variable to access the function directory.

    Property name Description
    EXECUTION_CONTEXT_FUNCTIONDIRECTORY Provides the current function directory (e.g. when running on Azure, d:homesitewwwrootHttpTrigger1)
    $functionDir = $env:EXECUTION_CONTEXT_FUNCTIONDIRECTORY
    Write-Host $functionDir
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search