从多个文件创建powershell模块,使用模块引用

时间:2017-06-12 22:34:16

标签: powershell

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

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

看起来点源并不是一个好主意。也没有制作组件文件(“foo”和“bar”)psm1文件。这是psd1文件中“ScriptsToProcess”字段背后的想法吗?

我是否在想这个错误(非“PowerShelly”)?我应该把所有东西都放到一个psm1吗?

3 个答案:

答案 0 :(得分:9)

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

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

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

#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

然后,您还应在FunctionsToExport设置下的.psd1模块清单文件中明确列出您的公共函数名称。这样做可以发现这些功能,并且模块在使用时可以自动加载。

答案 1 :(得分:2)

由于我最近不得不自己完成此操作,因此我正在分享我的解决方案。我最近开始在psm1文件中对功能进行分组。可以将它们编译成具有单个清单的单个模块。

这使我具有可以与多个模块打包的功能组。

Write-BarFunctions.psm1

Function Write-Bar {
  return "Bar"
}

Function Write-Baz {
    return "Baz"
}

Write-FooFunctions.psm1

Function Write-Foo {
    return "Foo"
}

Function Write-FooBar {
    $foo = Write-Foo
    $bar = Write-Bar
    return ("{0}{1}" -f $foo, $bar)
}

Function Write-FooBarBaz {
    $foobar = Write-FooBar
    $baz = Write-Baz
    return ("{0}{1}" -f $foobar, $baz)
}

将其合并为一个模块,如下所示: (为便于阅读而格式化)

New-ModuleManifest 
    -Path .\Write-FooBarBazCombos 
    -NestedModules @('.\FooFunctions\Write-FooFunctions.psm1', '.\BarFunctions\Write-BarFunctions.psm1') 
    -Guid (New-Guid) 
    -ModuleVersion '1.0.0.0' 
    -Description 'demonstrate multiple psm1 files as 1 powershell module with 1 powershell module manifest' 
    -PowerShellVersion $PSVersionTable.PSVersion.ToString() 
    -FunctionsToExport @('Write-Foo', 'Write-Bar','Write-FooBar', 'Write-FooBarBaz')


PowerShell输出:

PS C:\LWC\scripting-misc\module-manifest-multiple-files-example> New-ModuleManifest -Path .\Write-FooBarBazCombos.psd1
-NestedModules @('.\Write-FooFunctions.psm1', '.\Write-BarFunctions.psm1') -Guid (New-Guid) -ModuleVersion '1.0.0.0' -D
escription 'demonstrate multiple psm1 files as 1 powershell module with 1 powershell module manifest' -PowerShellVersio
n $PSVersionTable.PSVersion.ToString() -FunctionsToExport @('Write-Foo', 'Write-Bar','Write-FooBar', 'Write-FooBarBaz')

PS C:\LWC\scripting-misc\module-manifest-multiple-files-example> Import-Module .\Write-FooBarBazCombos.psd1
PS C:\LWC\scripting-misc\module-manifest-multiple-files-example> Get-Command -Module Write-FooBarBazCombos

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Function        Write-Bar                                          1.0.0.0    Write-FooBarBazCombos
Function        Write-Foo                                          1.0.0.0    Write-FooBarBazCombos
Function        Write-FooBar                                       1.0.0.0    Write-FooBarBazCombos
Function        Write-FooBarBaz                                    1.0.0.0    Write-FooBarBazCombos
  • 请注意,Write-Baz不会在导入的模块中公开,因为它已从FunctionsToExport参数中排除,因此Write-FooBarBaz将出错(有意显示行为)。
PS C:\LWC\scripting-misc\module-manifest-multiple-files-example> Write-FooBar
FooBar

目录中剩下的内容:

PS C:\LWC\scripting-misc\module-manifest-multiple-files-example> Get-ChildItem | Select-Object Name

Name
----
Write-BarFunctions.psm1
Write-FooBarBazCombos.psd1
Write-FooFunctions.psm1

附录-我在另一个问题中扩展了此答案-此处:

https://stackoverflow.com/a/56171985/7710456

答案 2 :(得分:1)

@瑞安 我类似地认为,点源并不是这里的最佳选择,但是我不确定。我也使用过NestedModules方法,但是遇到了一个特定的问题。我在这里问了一个问题: PowerShell module, call function in NestedModule from another NestedModule

总而言之,我发现PrimaryModule可以调用任何NestedModule中的任何函数。但是一个NestedModule无法调用另一个NestedModule中的函数。

将代码拆分为许多逻辑文件是Developer 101的基础知识。因此,令我感到非常惊讶的是,没有一种标准的处理方法。

这里的任何帮助都非常感谢。请阅读链接的问题,它提供了大量详细信息。是否必须使用网点采购的共识?因为我发现模块清单方法非常有限地分割代码。

相关问题