我列出要在我的powershell模块清单中导出的变量实际上并不是导出的

use*_*801 7 variables powershell module export manifest

我正在创建一个脚本模块并使用清单来导出脚本成员并设置其他模块属性.我已经跟踪了我发现的每个示例清单,甚至使用New-ModuleManifest来创建具有我想要的属性的基线清单,我仍然无法获得我想要导出到实际导出的变量.

这里的目的是在清单中执行所有声明,而不必Export-ModuleMember在模块脚本中使用.

这是脚本模块:

#
# Widget.psm1
#

[System.Random]$rGen = New-Object System.Random;
[string]$WidgetBaseName = $null;
[string]$WidgetColor = "Blue";

function Get-WidgetName
{
    param
    (
        [Parameter(Mandatory=$true)]
        [string]$widgetName,
        [switch]$appendRandomNumber
    )

    if (![string]::IsNullOrEmpty($WidgetBaseName))
    {
        $widgetName = $WidgetBaseName + $widgetName;
    }

    if ($appendRandomNumber)
    {
        return [string]::Format("{0}{1:D4}", $widgetName, $rGen.Next(10000));
    }
    else
    {
        return $widgetName;
    }
}

function Get-WidgetBlessing()
{
    return [string]::Format("A thousand blessings upon your {0} widget!", $WidgetColor);
}
Run Code Online (Sandbox Code Playgroud)

这是显而易见的:

#
# Widget.psd1
#

@{

# Script module or binary module file associated with this manifest
RootModule = 'Widget.psm1'

# Version number of this module.
ModuleVersion = '0.0.0.1'

# ID used to uniquely identify this module
GUID = 'c4437164-ea47-4148-97ed-48737bd5824d'

# Author of this module
Author = 'Widget Developer'

# Company or vendor of this module
CompanyName = 'Fictional Company Inc.'

# Copyright statement for this module
Copyright = '(c) 2016 Fictional Company. All rights reserved.'

# Description of the functionality provided by this module
Description = 'Widget Module'

# Minimum version of the Windows PowerShell engine required by this module
# PowerShellVersion = ''

# Name of the Windows PowerShell host required by this module
# PowerShellHostName = ''

# Minimum version of the Windows PowerShell host required by this module
# PowerShellHostVersion = ''

# Minimum version of the .NET Framework required by this module
# DotNetFrameworkVersion = ''

# Minimum version of the common language runtime (CLR) required by this module
# CLRVersion = ''

# Processor architecture (None, X86, Amd64) required by this module
# ProcessorArchitecture = ''

# Modules that must be imported into the global environment prior to importing this module
# RequiredModules = @()

# Assemblies that must be loaded prior to importing this module
# RequiredAssemblies = @()

# Script files (.ps1) that are run in the caller's environment prior to importing this module
# ScriptsToProcess = @()

# Type files (.ps1xml) to be loaded when importing this module
# TypesToProcess = @()

# Format files (.ps1xml) to be loaded when importing this module
# FormatsToProcess = @()

# Modules to import as nested modules of the module specified in RootModule/ModuleToProcess
# NestedModules = @()

# Functions to export from this module
FunctionsToExport = @( "Get-WidgetName", "Get-WidgetBlessing" )

# Cmdlets to export from this module
# CmdletsToExport = @()

# Variables to export from this module
VariablesToExport = 'WidgetBaseName', 'WidgetColor'

# Aliases to export from this module
AliasesToExport = '*'

# List of all modules packaged with this module
# ModuleList = @()

# List of all files packaged with this module
# FileList = @()

# Private data to pass to the module specified in RootModule/ModuleToProcess
# PrivateData = ''

# HelpInfo URI of this module
# HelpInfoURI = ''

# Default prefix for commands exported from this module. Override the default prefix using Import-Module -Prefix.
# DefaultCommandPrefix = ''

}
Run Code Online (Sandbox Code Playgroud)

要检查导出的成员,请在PowerShell窗口中运行以下命令:

Import-Module Widget
Get-Module -Name Widget | fl
Run Code Online (Sandbox Code Playgroud)

这是输出.注意任何导出变量的显着缺失.

Name              : Widget
Path              : D:\SRE\PowerShell\Widget\Widget.psm1
Description       : Widget Module
ModuleType        : Script
Version           : 0.0.0.1
NestedModules     : {}
ExportedFunctions : {Get-WidgetBlessing, Get-WidgetName}
ExportedCmdlets   : 
ExportedVariables : 
ExportedAliases   : 
Run Code Online (Sandbox Code Playgroud)

为什么是这样?我正在使用New-ModuleManifest生成的清单(New-ModuleManifest -VariablesToExport WidgetBaseName WidgetColor).该工具是否已损坏,或清单中是否存在导致此行为的其他内容?

更新:

我在模块脚本的末尾添加了以下行:

Export-ModuleMember -Variable WidgetBaseName, WidgetColor
Run Code Online (Sandbox Code Playgroud)

VariablesToExport在清单文件中将值更改为"*".

现在当我导入模块并按上述方法运行检查时,我得到了这个:

Name              : Widget
Path              : D:\SRE\PowerShell\Widget\Widget.psm1
Description       : Widget Module
ModuleType        : Script
Version           : 0.0.0.1
NestedModules     : {}
ExportedFunctions : 
ExportedCmdlets   : 
ExportedVariables : {WidgetBaseName, WidgetColor}
ExportedAliases   : 
Run Code Online (Sandbox Code Playgroud)

...我的导出功能在哪里?

Jas*_*oyd 6

因此,为了完整起见,也为了将来遇到这个问题的任何人,我将总结我们在对 OP 问题的评论中进行的对话。

默认情况下,所有函数都是从模块导出的,没有其他成员表现出这种行为。就好像Export-ModuleMember -Function *从模块内部隐式调用一样。然后,您可以进一步限制通过ExportedFunctions模块清单中的键导出哪些函数,例如ExportedFunctions : {Get-WidgetBlessing, Get-WidgetName}.

如果要导出变量或别名,则必须显式地添加对模块的调用Export-ModuleMember -Variable <what you want to export>Export-ModuleMember -Alias <what you want to export>在模块中。但是,一旦您添加了Export-ModuleMember对隐式调用的显式调用,Export-ModuleMember -Function *就不再发生。Export-ModuleMember -Function *如果您添加了一个其他显式调用,您必须记住显式添加到您的模块,Export-ModuleMember否则您的函数将不再继续导出。