eps icon indicating copy to clipboard operation
eps copied to clipboard

Variables whose name is other than `name` or `name_value` don't work in script

Open ooker777 opened this issue 2 years ago • 1 comments

Say I have this test.esp:

Without underscore: <%= $name %>
With underscore: <%= $name_value %>

If I run it directly on shell, it works fine:

$name = 'ABC'
$name_value = 'ABC'

Invoke-EpsTemplate -Path .\test.esp
Without underscore: ABC
With underscore: ABC

But if I put them in a build.ps1 script:

$name = 'ABC'
$name_value = 'ABC'
Invoke-EpsTemplate -Path .\test.esp

Then it doesn't work:

.build.ps1
Without underscore: ABC
With underscore:

This is also true for any variable name I try, except name and name_value. Even name_values doesn't work. Why is that?

ooker777 avatar Oct 10 '23 16:10 ooker777

This has nothing to do with underscore, and a lot to do with scopes. If you close your terminal, and then execute your ./build.ps1 in a freshly opened one, you will see that neither of the vars is displayed. This is because in this case they both will be in the script scope, and when you tested it earlier, you had $name in the global scope.

It should be noted, that this behaviour is because modules, apparently, have their own scopes (while still sharing the global scope). If you instead of loading the module simple import each of the functions in this repo, it will work.

There are a few workarounds:

Use the Binding parameter

Invoke-EpsTemplate -Path .\test.esp -binding @{ name = 'ABC';  name_value = 'ABC'}

Invoke your script in the global scope

Both

pwsh ./build.ps1

and

. ./build.ps1

will work.

Use gloably scoped variables explicitly

$global:name = 'ABC'
$global:name_value = 'ABC'
Invoke-EpsTemplate -Path .\test.esp

AndrewSav avatar Mar 13 '25 07:03 AndrewSav