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

    • KB0011 - Are licenses different between Production, QA and Test/Development servers?

      Update January 24th, 2023 Adam recently summarized the Developer's license per below: The only real limitation on the developer license is that it cannot be accessed remotely. The server is only available on loopback when using the dev license. If ...
    • PowerShell Universal Service crashes on startup after an upgrade to 1.4.6

      Version: PowerShell Universal 1.4.6 Issue The PowerShell Universal service will crash with the following error in Event Viewer.  Application: Universal.Server.exe CoreCLR Version: 4.700.19.56402 .NET Core Version: 3.1.0 Description: The process was ...
    • KB0020 - Firewall Configuration for PowerShell Universal

      Purpose The purpose of this document is to provide information about the necessary filewall settings required by PowerShell Universal. Overview PowerShell Universal does not offer a built in firewall. We recommend using standard firewalls available ...
    • PowerShell Universal cmdlets return a 404 over HTTPS

      Version: 1.4 PowerShell Version: Windows PowerShell 5.1 Problem When issue commands against the PowerShell Universal Management API (such as Get-UAJob, Get-UAScript, etc), the cmdlet will return a 404 error. This can happen when running scripts ...
    • PowerShell Universal crashes after upgrade to 1.5

      Version: 1.5 Issue: The PowerShell Universal service will not start properly when upgrading from 1.4 to 1.5. The following error is shown in the log.  Unhandled exception. System.InvalidCastException: Unable to cast object of type ...