microsoft / Requirements

PowerShell framework for declaratively defining and idempotently imposing system configurations
MIT License
159 stars 26 forks source link

Dot Sourced variables only exist within context of current Requirement #75

Closed mmcdermo2 closed 2 years ago

mmcdermo2 commented 2 years ago

I've only just started using this module in the past few days, so I'll apologize in advance if this is something that's already covered in the documentation that I just haven't seen.

I've created an external file named ResourceVariables.ps1, so that anybody using my script can easily plug in their own variables without having to make any changes to the actual requirements script file (CreateResources.ps1)

Within CreateResources.ps1, I'm dot sourcing ResourceVariables.ps1 to import it's variables. However, the variables only exist within the current Resource block, ie:

@(

    @{
        Describe = "First Requirement"
        Set = { . .\ResourceVariables.ps1;
                Write-Host "Import Variables block value of var1 is $var1"
                }
    }

    @{
        Describe = "Second Requirement"
        Set = { Write-Host "Use Variables block value of var1 is $var1" }

    }
) | Invoke-Requirement | Format-Verbose

In the above example, only "First Requirement" has a value for $var1, but "Second Requirement" does not.

If I declare $var1 within CreateResource.ps1 instead, then both requirements have access to the $var1. But similarly, if I set the value of $var1 within "First Requirement", then "Second Requirement" does not see that value.

I presume this has something to do with requirement nesting, which is no longer supported. But I don't see anything in the Patterns section that would help resolve this issue.

Can anybody give me some guidance on where I'm going wrong ?

mmcdermo2 commented 2 years ago

Typical, just as I posted this I found #73 which pretty much answers my question. I think I'll just have to do the dot sourcing at the top of my CreateResources script rather than doing it as a requirement