我在 powershell 模块清单中列出的要导出的变量实际上并未导出
Variables I listed to export in my powershell module manifest aren't actually exporting
我正在创建脚本模块并使用清单导出脚本成员并设置其他模块属性。我几乎遵循了我发现的每个示例清单,甚至使用 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 window 中执行了以下命令:
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 :
...我导出的函数去哪儿了?
因此,为了完整起见,也为了将来遇到这个问题的任何人,我将总结我们在对 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 *
显式添加到您的模块,否则您的函数将不再继续导出。
实际上注意到有效响应与文档相反。
FunctionsToExport
Specifies the functions to export from this module, for best
performance, do not use wildcards and do not delete the entry, use an
empty array if there are no functions to export. By default, no
functions are exported. You can use this key to list the functions
that are exported by the module. The module exports the functions to
the caller's session state. The caller's session state can be the
global session state or, for nested modules, the session state of
another module. When chaining nested modules, all functions that are
exported by a nested module will be exported to the global session
state unless a module in the chain restricts the function by using the
FunctionsToExport key. If the manifest exports aliases for the
functions, this key can remove functions whose aliases are listed in
the AliasesToExport key, but this key cannot add function aliases to
the list. Example: FunctionsToExport = @("function1", "function2",
"function3")
VariablesToExport
Specifies the variables that the module exports to the caller's
session state. Wildcard characters are permitted. By default, all
variables ('*') are exported. You can use this key to restrict the
variables that are exported by the module. The caller's session state
can be the global session state or, for nested modules, the session
state of another module. When you are chaining nested modules, all
variables that are exported by a nested module will be exported to the
global session state unless a module in the chain restricts the
variable by using the VariablesToExport key. If the manifest also
exports aliases for the variables, this key can remove variables whose
aliases are listed in the AliasesToExport key, but this key cannot add
variable aliases to the list. Example: VariablesToExport =
@('$MyVariable1', '$MyVariable2', '$MyVariable3')
我正在创建脚本模块并使用清单导出脚本成员并设置其他模块属性。我几乎遵循了我发现的每个示例清单,甚至使用 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 window 中执行了以下命令:
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 :
...我导出的函数去哪儿了?
因此,为了完整起见,也为了将来遇到这个问题的任何人,我将总结我们在对 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 *
显式添加到您的模块,否则您的函数将不再继续导出。
实际上注意到有效响应与文档相反。
FunctionsToExport
Specifies the functions to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no functions to export. By default, no functions are exported. You can use this key to list the functions that are exported by the module. The module exports the functions to the caller's session state. The caller's session state can be the global session state or, for nested modules, the session state of another module. When chaining nested modules, all functions that are exported by a nested module will be exported to the global session state unless a module in the chain restricts the function by using the FunctionsToExport key. If the manifest exports aliases for the functions, this key can remove functions whose aliases are listed in the AliasesToExport key, but this key cannot add function aliases to the list. Example: FunctionsToExport = @("function1", "function2", "function3")
VariablesToExport
Specifies the variables that the module exports to the caller's session state. Wildcard characters are permitted. By default, all variables ('*') are exported. You can use this key to restrict the variables that are exported by the module. The caller's session state can be the global session state or, for nested modules, the session state of another module. When you are chaining nested modules, all variables that are exported by a nested module will be exported to the global session state unless a module in the chain restricts the variable by using the VariablesToExport key. If the manifest also exports aliases for the variables, this key can remove variables whose aliases are listed in the AliasesToExport key, but this key cannot add variable aliases to the list. Example: VariablesToExport = @('$MyVariable1', '$MyVariable2', '$MyVariable3')