从多个文件创建 powershell 模块,引用模块 [英] Creating powershell modules from multiple files, referencing with module

查看:74
本文介绍了从多个文件创建 powershell 模块,引用模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用单独的源文件创建了一个 PowerShell 脚本模块.从其他内部源文件引用模块内部源函数的规范方法是什么?

I creating a PowerShell script module using separate source files. What is the canonical way to reference source functions internal to the module from other internal source files?

例如,如果我的模块是从文件foo"和bar"中的 PS 源代码创建的;并且foo"中的函数需要调用bar"中的函数,最好的方法是什么?

For example if my module is created from PS source code in files "foo" and "bar"; and a function in "foo" needs to call a function in "bar", what is the best way to do that?

dot-sourcing 似乎不是一个好主意.也不会制作组件文件(foo"和bar")psm1 文件.这是 psd1 文件中ScriptsToProcess"字段背后的想法吗?

It doesn't seem like dot-sourcing would be a good idea. Nor does making the component files ("foo" and "bar") psm1 files. Is this the idea behind the "ScriptsToProcess" field in the psd1 file?

我是否在考虑这个错误(非PowerShelly")?我应该将所有内容都转储到一个 psm1 中吗?

Am I thinking about this wrong (non-"PowerShelly")? Should I just dump everything into a single psm1?

推荐答案

我个人遵循了 RamblingCookieMonster 在他的博客中提出的做法:http://ramblingcookiemonster.github.io/Building-A-PowerShell-Module/

I've personally followed the practice laid out by RamblingCookieMonster in his blog here: http://ramblingcookiemonster.github.io/Building-A-PowerShell-Module/

用于将您的函数组织到子文件夹 \Public\Private 下的单独 .ps1 文件中.Public 包含用户应该能够直接调用的函数,Private 是 PowerShell 内部的函数.

Which is to organise your functions in to separate .ps1 files under sub-folders \Public and \Private. Public contains the functions the user should be able to call directly, Private is the functions internal to PowerShell.

然后在 .psm1 文件中通过循环和点源加载函数,如下所示:

Then in the .psm1 file you load the functions via a loop and dot sourcing as follows:

#Get public and private function definition files.
    $Public  = @( Get-ChildItem -Path $PSScriptRoot\Public\*.ps1 -ErrorAction SilentlyContinue )
    $Private = @( Get-ChildItem -Path $PSScriptRoot\Private\*.ps1 -ErrorAction SilentlyContinue )

#Dot source the files
    Foreach($import in @($Public + $Private))
    {
        Try
        {
            . $import.fullname
        }
        Catch
        {
            Write-Error -Message "Failed to import function $($import.fullname): $_"
        }
    }

# Here I might...
    # Read in or create an initial config file and variable
    # Export Public functions ($Public.BaseName) for WIP modules
    # Set variables visible to the module and its functions only

Export-ModuleMember -Function $Public.Basename

  • 此示例的来源:https://githubRCookieMonster//PSStackExchange/blob/db1277453374cb16684b35cf93a8f5c97288c41f/PSStackExchange/PSStackExchange.psm1
  • 然后,您还应该在 FunctionsToExport 设置下的 .psd1 模块清单文件中明确列出您的公共函数名称.这样做可以让这些函数被发现,并且模块在使用时可以自动加载.

    You should then also explicitly list your Public function names in your .psd1 module manifest file under the FunctionsToExport setting. Doing this allows these functions to be discoverable and the module to be auto-loaded when they are used.

    这篇关于从多个文件创建 powershell 模块,引用模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆