【问题标题】:Variables I listed to export in my powershell module manifest aren't actually exporting我在我的 powershell 模块清单中列出的要导出的变量实际上并未导出
【发布时间】:2016-11-15 07:33:51
【问题描述】:

我正在创建一个脚本模块并使用清单来导出脚本成员并设置其他模块属性。我几乎遵循了我找到的每个示例清单,甚至使用 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);
}

这是清单:

#
# 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 = ''

}

要检查导出了哪些成员,我在 PowerShell 窗口中运行以下命令:

Import-Module Widget
Get-Module -Name Widget | fl

这是输出。请注意明显没有任何导出的变量。

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   : 

这是为什么?我正在使用 New-ModuleManifest 生成的清单 (New-ModuleManifest -VariablesToExport WidgetBaseName WidgetColor)。该工具是否损坏,或者清单中是否有其他东西导致了这种行为?

更新:

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

Export-ModuleMember -Variable WidgetBaseName, WidgetColor

我在清单文件中将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   : 

...我导出的函数到哪里去了?

【问题讨论】:

  • 没有损坏;你必须使用Export-ModuleMember;查看链接的副本。
  • 这是一个链接,扩展了@briantist 所说的内容:itidea.nl/index.php/…。我以前从未尝试过导出变量;那个 SO 帖子和这个链接中描述的行为绝对是违反直觉的恕我直言。
  • @JasonBoyd 这是一篇很棒的文章
  • 我不同意这是与其他文章的重复。另一篇文章仅讨论使用Export-ModuleMember 导出,根本不讨论清单。这里的要点是不能使用清单来声明要导出的变量吗?

标签: powershell variables module export manifest


【解决方案1】:

因此,为了完整起见,并且对于将来遇到此问题的任何人,我将把我们在 cmets 中的对话总结为 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 添加了一个其他显式调用,您必须记住将Export-ModuleMember -Function * 显式添加到您的模块中,否则您的函数将不再继续被导出。

【讨论】:

  • 我实际上更喜欢反过来做:调用Export-ModuleMember 来限制从psm1 模块导出的内容,然后在清单中将这些字段保留为'*'。这意味着我可以在一个文件中管理所有模块的事物名称,而不是来回跳转。
【解决方案2】:

实际上注意到有效响应与文档相反。

来自Microsoft

FunctionsToExport

指定要从此模块导出的函数,以达到最佳效果 性能,不要使用通配符,不要删除条目,使用 如果没有要导出的函数,则为空数组。 默认情况下,没有 函数被导出。您可以使用此键列出功能 由模块导出。该模块将函数导出到 调用者的会话状态。调用者的会话状态可以是 全局会话状态,或者,对于嵌套模块,会话状态 另一个模块。当链接嵌套模块时,所有的函数 由嵌套模块导出将被导出到全局会话 除非链中的模块通过使用 FunctionsToExport 键。如果清单导出别名 函数,该键可以删除别名在 AliasesToExport 键,但该键不能添加函数别名 名单。示例: FunctionsToExport = @("function1", "function2", "函数3")

VariablesToExport

指定模块导出到调用者的变量 会话状态。允许使用通配符。 默认情况下,所有 变量 ('*') 被导出。您可以使用此键来限制 模块导出的变量。调用者的会话状态 可以是全局会话状态,或者对于嵌套模块,会话 另一个模块的状态。当您链接嵌套模块时,所有 嵌套模块导出的变量将被导出到 全局会话状态,除非链中的模块限制 变量通过使用 VariablesToExport 键。如果清单也 导出变量的别名,此键可以删除其变量 AliasesToExport 键中列出了别名,但该键不能添加 列表的变量别名。示例:VariablesToExport = @('$MyVariable1', '$MyVariable2', '$MyVariable3')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-25
    • 1970-01-01
    相关资源
    最近更新 更多