Variables created within modules are not available in Universal API endpoints

Variables created within modules are not available in Universal API endpoints

Version: 1.4+
When using environments that import modules, you will not be able to access variables that are either global or export from modules. 

Affected Configuration

The affected configuration includes modules that export variables either with Export-ModuleMember, a manifest or through the use of $Global. 
For example, the following module creates a $Global variable. 
  1. $Global:MyVar = @{ Timestamp = Get-Date } 
In the PowerShell Universal configuration, you may import this module so it's available in all users of the environment you configure. For example, you could have an envrionments.ps1 like so. 
Environments.ps1
  1. New-PSUEnvironment -Name "module" -Path "powershell.exe" -Modules @('C:\Users\adamr\Desktop\module.psm1')
If you configure the API to use this environment, the $Global:MyVar will not be available.

Settings.ps1
  1. Set-PSUSetting -LogLevel "Error" -Telemetry -ApiEnvironment "module"
Endpoints.ps1
  1. New-PSUEndpoint -Url "/test" -Endpoint { 
    $Global:MyVar
     }

Workaround

To work around this issue, you can define a function in your module to return the variable. 
  1. $MyVar = @{ Timestamp = Get-Date } 
  2. function Get-MyVar {
  3.        $MyVar
  4. }
  5. Export-ModuleMember -Function "Get-MyVar"
In your API endpoint, you can then call the Get-MyVar function to return the value of the variable. The module is not reimported and you will notice that the time stamp does not change. 
  1. New-PSUEndpoint -Url "/test" -Endpoint { 
    (Get-MyVar)["Timestamp"]
    }