1

我在 PowerShell 中有许多高级功能,它们处理版本号,这是我们配置模块的核心部分。

当用户导入配置模块时,这些功能应该可供他们使用。我可以将所有功能复制到配置模块中,但如果它像辅助模块一样保存,那将非常好组织。

最终用户应该只导入主配置模块,但这个帮助模块应该包含在其中。有没有办法做到这一点?

4

2 回答 2

1

您可以在Module Manifest中将帮助模块指定为“嵌套模块” 。

@{
  ModuleToProcess   = 'Configuration.psm1'
  ModuleVersion     = '1.0'
  GUID              = '7ec463d6-de22-40bb-a505-1efcb3b22b73'
  Author            = 'Ansgar Wiechers'
  Description       = 'Configuration Module'
  PowerShellVersion = '2.0'

  FunctionsToExport = '*'
  CmdletsToExport   = '*'
  VariablesToExport = '*'
  AliasesToExport   = '*'

  NestedModules     = 'Helper'
}

辅助模块实际上不必嵌套。您可以将两个模块作为单独的模块放置:

WindowsPowerShell
`-Modules
  +-Configuration
  | +-Configuration.psd1
  | `-Configuration.psm1
  `-Helper
    +-Helper.psd1
    `-Helper.psm1
于 2015-09-09T21:22:57.540 回答
0

您可以通过创建另一个 PS 模块路径并将它们放置在那里来使它们都作为 cmdlet 可用 - 这将在每个 PS 会话中对所有用户可用。他们所要做的Import-Module一切,所有功能都将可用。

在 Modules 中,您可以在其中包含与 .psm1 文件名称相同的文件夹,例如Modules/Configuration/Configuration.psm1& Modules/Helper/Helper.psm1

if (!(Test-Path $Profile.AllUsersAllHosts)) {
  $profile_new1 = New-Item -Type File -Path $Profile.AllUsersAllHosts -Force
  Add-Content $profile_new1 '$env:PSModulePath = $env:PSModulePath + ";C:\Temp\Modules"'
} else {
  $profile_exist = Get-Item $Profile.AllUsersAllHosts
  Add-Content $profile_exist '$env:PSModulePath = $env:PSModulePath + ";C:\Temp\Modules"'
}
于 2015-09-09T15:52:01.527 回答