powershell

Why are variables NOT exported when list under VariablesToExport in the PS Manifest of a module


The manifest variable export

VariablesToExport = @(
        "ERROR_SUCCESS_REBOOT_REQUIRED",
        "ERROR_SUCCESS",
        "ERROR_INVALID_FUNCTION",
        "WAIT_TIMEOUT",
        "ERROR_FAIL"
    )

The variables are defined in a PSM1 file that is listed under NestedModules

$ERROR_SUCCESS  = [ordered]@{
    Number         = 0
    String         = '0'  # The thing the CSV interfaces return 
    HexString      = '0x00000000'                  
}

But they are not accessible from where the module gets loaded.


Solution

  • You need to call Export-ModuleMember from the script module:

    If a script module does not include an Export-ModuleMember command, the functions and aliases in the script module are exported, but the variables are not. When a script module includes Export-ModuleMember commands, only the members specified in the Export-ModuleMember commands are exported.

    (emphasis added)

    In other words: Declaring the variable name(s) in the manifest allows for discovery of module contents:

    Get-Module -ListAvailable |Where-Object { $_.ExportedVariables.ContainsKey('abc') }
    

    ... but doesn't actually affect nested script modules since variables default to not export.

    $ERROR_SUCCESS  = [ordered]@{
        Number         = 0
        String         = '0'  # The thing the CSV interfaces return 
        HexString      = '0x00000000'                  
    }
    
    Export-ModuleMember -Variable ERROR_SUCCESS