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

标签 powershell

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

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

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

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

最佳答案

由于我最近不得不自己做这件事,所以我正在分享我的解决方案。我最近开始在 psm1 文件中对函数进行分组。这些可以编译成具有单个 list 的单个模块。

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

写 BarFunctions.psm1

Function Write-Bar {
  return "Bar"
}

Function Write-Baz {
    return "Baz"
}

写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

    关于powershell - 从多个文件创建powershell模块,引用模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44509704/

    相关文章:

    powershell - Powershell-SQL查询结果转换为具有属性的变量

    mysql - PowerShell:ODBC DSN 连接中的多个查询

    windows - PowerShell 3 的文件扩展名

    Windows Server 2019 上的 Azure 管道 powershell 和 git 在输出中出现错误

    powershell - 使用 PowerShell 将二进制整数写入文件

    powershell - 在 IIS7.5 中使用 Powershell 设置启用父路径

    Powershell 参数绑定(bind) ByPropertyName 和 ByValue

    powershell - 调用命令中的导入模块

    sql-server - ISE无法使用Invoke-Sqlcmd,但可以在常规Powershell主机中使用

    powershell - 如何通过 tf.exe 从 Azure DevOps Agent 上的第二个项目获取文件?