2014-10-08 60 views
6

如果我使用嵌套模塊創建了清單模塊,則在第一個模塊之後的所有嵌套模塊中導出的函數不會出現在可用命令列表中,不會觸發模塊自動加載。在腳本模塊中調用嵌套模塊中的函數並不總是觸發模塊自動加載

當我運行「Get-Module-ListAvailable」時,它們也不會出現。

只有從第一個嵌套模塊導出的函數出現在命令列表中。

如果我明確導入模塊,則所有導出的函數都可用。

在下面的示例中,Update-LegacyServices在顯式導入模塊之前不可用。

我可以使它工作的唯一方法是將我的模塊文件重命名爲以ps1而不是psm1結尾,並將它們包含在ScriptsToProcess中,這似乎是一個糟糕的主意。

模塊清單(PSD1)

@{ 

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

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

# ID used to uniquely identify this module 
GUID = 'c11d6aca-d531-4d06-a732-5fb95113357f' 

# Author of this module 
Author = 'luke' 

# Company or vendor of this module 
CompanyName = '' 

# Copyright statement for this module 
Copyright = '' 

# Description of the functionality provided by this module 
# Description = 'MyBudget Developer Powershell Module' 

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

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

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

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

# 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 = @('database\Database.psm1', 'build\Build.psm1') 

# Functions to export from this module 
#FunctionsToExport = '*' 

# Cmdlets to export from this module 
CmdletsToExport = '*' 

# Variables to export from this module 
VariablesToExport = '*' 

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

# List of all modules packaged with this module. 
ModuleList = @('database\Database.psm1', 'build\Build.psm1') 

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

} 

模塊1(構建\ Build.psm1)

function Update-LegacyServices() 
{ 
    echo "Update" 
} 

Export-ModuleMember -Function Update-LegacyServices 

模塊2(數據庫\ Database.psm1)

Function Get-Backup($directory, $name) 
{ 
    echo "Get-Backup" 
} 

Export-ModuleMember -Function Get-Backup 
+0

我想你必須從模塊清單中取消註釋'#FunctionsToExport ='*''。 – 2014-10-08 07:29:43

+0

你認爲還是你測試過?對我而言都沒有效果 – LukeN 2014-10-08 07:37:48

+0

正如我所說,我想。只是一種預感。如果它不起作用,那就這樣吧。 – 2014-10-08 07:42:25

回答

3

我有這個線在我的.psd1文件

FunctionsToExport = 'FuncFromMainPsm1 FuncFromSecondPsm1' 

這是我從Powershell的工具Visual Studio生成的行推斷:

# Functions to export from this module 
FunctionsToExport = '*' 

這引起了我的Get-Module -ListAvailable顯示什麼樣子正確的事

Script  1.0  MyMModule     FuncFromMainPsm1 FuncFromSecondPsm1 

但是,當我叫FuncFromSecondPsm1我會得到「術語‘FuncFromSecondPsm1’不是......」。

所以我改變了我的出口線

FunctionsToExport = @('FuncFromMainPsm1', 'FuncFromSecondPsm1') 

現在,這一切的作品。我可以在我的模塊加載後調用這兩個函數,無論是通過自動加載還是Import-Module

我試過這個有和沒有ModuleListFileList集合。他們沒有區別。

+0

這對我的Module Module工作正常=腳本。我有ModuleType = Manifest的另一個模塊,它不工作!需要更多調查 – 2015-06-04 02:16:45

+1

確認:在這種情況下,您似乎需要使用腳本清單。將其中一個腳本設置爲根模塊,然後其他腳本可以是嵌套模塊。標記爲根模塊的模塊與所有其他模塊之間沒有明顯差異。 – 2015-06-04 02:22:52

1

海蘭保羅,海蘭Luken,

掙扎了幾個小時就這個話題(與含蓄,並宣佈清單的方法)後,我選擇了自動生成的清單。 從本質上講,我寫了一個PS1腳本生成清單,分兩個步驟:

  1. 一些「業務」的邏輯,發現要公開
  2. 生成模塊清單什麼

恕我直言,這種方法的優勢:我'充分'瞭解和掌握模塊內容的曝光。

下面你可以找到一個PS代碼片段,遵循這種方法,希望它會受益於其他人。

# module discovery 
$rootModule = "WdCore"; 
$modules = Get-ChildItem *.psm1; 
$nestedmodulesNames = $modules | where { $_.BaseName -ne "WdCore"} | % { $_.BaseName } ; 

# functions discovery 
$modulesLines = $modules | Get-Content; 
$functionsLines = $modulesLines | where { $_.contains("Function") }; 

$functionNamePattern = '^Function\s+(?<name>([a-z][0-9|A-Z|-]+))\s?{.*$'; 
$functionNames = $functionsLines | where { $_ -match $functionNamePattern } | select { $Matches['name'] } | % { $_.' $Matches[''name''] '}; 

# generate manifest 
New-ModuleManifest ` 
    -Path ./WdTools.psd1 -RootModule $rootModule ` 
    -ModuleVersion '1.0' ` 
    -NestedModules $nestedmodulesNames ` 
    -FunctionsToExport $functionNames ` 
    -Guid '57D7F213-2316-4786-8D8A-3E4B9262B1E5' ` 
    -Author 'Blaise Braye' ` 
    -Description 'This module provides working directory tooling' ` 
    -PowerShellVersion '3.0' -ClrVersion '4.0';