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"]
    }


    • Related Articles

    • KB0041 - About Templates

      Purpose The purpose of this article is to provide a timeline around the Template functionality of PowerShell Universal. Background Templates were introduced to provide a starting point for newcomers to PowerShell Universal. They have since been ...
    • KB0071 - Apps and APIs restart when changing variables in PowerShell Universal

      Scope This applies to users who change variable values on their PowerShell Universal environments during runtime. This affects all versions of PowerShell Universal. Problem Apps and APIs will restart when variables are changed. This can cause users ...
    • KB0069 - PowerShell Universal Startup Process

      Purpose The purpose of this document is to outline the steps that PowerShell Universal takes when starting up. Process 1. Insert current product version and install date in database Updates the database with a record about the current product version ...
    • KB0063 - API, App or Job Not Starting

      Problem External processes do not start after upgrades or with the same configuration and script on different machines. Common errors in the log will include: [ERR] Dashboard process running but not response after 10 seconds. Terminating process. ...
    • KB0035 - How to add the Calendar and other components

      Purpose The purpose of this article is to demonstrate how to install and use the Calendar component (module) Background There is a growing list of add-on components (modules) for PowerShell Universal at ...