【问题标题】:Is it possible to have helper modules?是否可以有辅助模块?
【发布时间】:2015-09-09 11:55:31
【问题描述】:

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

当用户导入配置模块时,这些功能应该可供他们使用。我可以把所有的功能都复制到配置模块中,但是如果像辅助模块一样保存的话,会很好的组织起来。

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

【问题讨论】:

  • 您可以创建某种链接,即当导入主模块时,主模块内的代码将静默导入助手之一。 How to do this

标签: powershell powershell-3.0 powershell-module


【解决方案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

【讨论】:

    【解决方案2】:

    您可以通过创建另一个 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"'
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-10-27
      • 2013-01-20
      • 2016-06-01
      • 1970-01-01
      • 2011-09-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多