Skip to content

Variable Scoping Quirk #43

Description

@flytape8490

When invoke-epstemplate is called from within a function, it only sees variables defined outside the function unless they are specifically added with the Binding hashtable.

eps.txt:

<%= $value -%>

PowerShell script

Import-Module EPS
$value = "Outside"

Function FunctionTest
{
    $value = "Inside"
    Write-Host "From a function, the value of `$value is $value"
    Write-Host "From a function, the result from invoke is $(Invoke-EpsTemplate -Path "eps.txt")"
    Write-Host "From a function, the result from invoke WITH BINDING is $(Invoke-EpsTemplate -Path "eps.txt" -Binding @{"value"=$value})"
}

Write-Host "The value of `$value is $value"
Write-Host "The value from invoke is $(Invoke-EpsTemplate -Path "eps.txt")"

FunctionTest

Results:

> The value of $value is Outside
> The value from invoke is Outside
> From a function, the value of $value is Inside
> From a function, the value from invoke is Outside
> From a function, the value from invoke WITH BINDING is Inside

Expected results:

> The value of $value is Outside
> The value from invoke is Outside
> From a function, the value of $value is Inside
> From a function, the value from invoke is Inside
> From a function, the value from invoke WITH BINDING is Inside

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions