如何管理多文件Powershell模块中脚本之间的依赖关系?

发布于 2024-11-13 06:22:15 字数 645 浏览 8 评论 0原文

我有大量的 powershell 代码是我在一个漫长的项目过程中编写的;这些脚本执行各种各样的功能,并且其中大多数在某种程度上依赖于项目范围内的其他脚本。目前,这项工作由几个文件组成,每个文件包含许多函数。最初,为了使用这些脚本,所有脚本文件都被随意地点源到环境中。

但是,我最近了解到 Powershell 2.0 引入了模块,我想以这种方式将这些脚本一起部署。由于模块的内容全部加载在一起,因此我想将文件分开,以便每个脚本都有自己的文件,以帮助源代码控制。不过,我现在有点不清楚脚本之间的联系。

我已经做了一些测试,似乎可以将每个函数的 Export-ModuleMember 命令移动到单独的 .ps1 文件中;这看起来更像是声明自己作用域的函数,如 C# 中的公共和私有作用域。但是,执行此操作后,我的 .psm1 文件仅包含以下内容:

Get-ChildItem -recurse $psScriptRoot | where { $_.Extension -eq ".ps1" } | foreach { . $_.FullName }

这看起来正确吗?所有脚本都是点源的,并且所有脚本在该假设下相互引用。他们是否应该使用相对于 $psScriptRoot 的位置来相互引用?

有没有一种方法与这两种方法不同?有人可以提供建议吗?我对这些还不太了解。

I have a large amount of powershell code that I've written over the course of a long project; these scripts perform a wide variety of functions, and most of them depend in some way on others within the scope of the project. Right now, the work is made up of a couple of files containing many functions each. Originally, in order to work with these scripts all the script files were sort of haphazardly dot-sourced into the environment.

However, I've learned recently that Powershell 2.0 introduces modules, and I would like to deploy these scripts together that way. Since a module's contents are all loaded together, I would like to split apart my files so that each script has its own file, in order to aid source control. However, I'm a little unclear about the connections between the scripts now.

I've done some testing and it seems that it's ok to move the Export-ModuleMember command for each function to the individual .ps1 files; this seems more like functions declaring their own scope like public and private scoping in C#. However, after doing that my .psm1 file contains nothing but this:

Get-ChildItem -recurse $psScriptRoot | where { $_.Extension -eq ".ps1" } | foreach { . $_.FullName }

Does that seem right? All the scripts are being dot sourced, and all the scripts refer to each other under that assumption. Should they instead refer to each other using their locations relative to $psScriptRoot?

Is there a way different than both these ways? Can anyone offer advice? I don't know much about these yet.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

π浅易 2024-11-20 06:22:15

我见过一种类似的技术,其中每个 .ps1 文件都包含一个函数,并且这些函数源自 WPK 模块和 PSRemoteRegistry 模块中使用的 PSM1 文件。

这一行是 PSRemoteRegistry 模块:

Get-ChildItem -Path $PSScriptRoot\*.ps1 | Foreach-Object{ . $_.FullName }

我想说我喜欢这种技术而不是拥有一个巨大的函数脚本文件。

I've seen a similar technique where each .ps1 file contains one function and the functions are sourced in the PSM1 file used in the WPK module and PSRemoteRegistry modules.

This line is the PSRemoteRegistry module:

Get-ChildItem -Path $PSScriptRoot\*.ps1 | Foreach-Object{ . $_.FullName }

I would say I like the technique over having one giant script files of functions.

夜未央樱花落 2024-11-20 06:22:15

您还可以考虑创建一个清单(我实际上不知道您是否需要带有 psd1 的 psm1)。

这是我对清单的使用:

New-ModuleManifest `
    -Path Fiddler.psd1 `
    -Author "Niklas Goude" `
    -CompanyName "http://www.powershell.nu/" `
    -ModuleVersion 1.0 `
    -Description "Module from http://www.powershell.nu/2011/03/14/fiddler/</a> - psd1 created by Matt @ amonskeysden.tumblr.com" `
    -FormatsToProcess @() `
    -RequiredAssemblies @("Fiddler.dll") `
    -NestedModules @() `
    -Copyright "" `
    -ModuleToProcess "Fiddler.psm1" `
    -TypesToProcess @() `
    -FileList @("Fiddler.psm1","Fiddler.dll")

我认为您问题的答案是将您的文件列表包含在 FileList 参数中。

我在这里写了一些我的发现(包括 MS 资源的链接):

http:// amonkeysden.tumblr.com/post/5127684898/powershell-and-fiddler

you could also look at creating a manifest (I don't actually know if you need a psm1 with a psd1).

Here was my use of a manifest:

New-ModuleManifest `
    -Path Fiddler.psd1 `
    -Author "Niklas Goude" `
    -CompanyName "http://www.powershell.nu/" `
    -ModuleVersion 1.0 `
    -Description "Module from http://www.powershell.nu/2011/03/14/fiddler/</a> - psd1 created by Matt @ amonskeysden.tumblr.com" `
    -FormatsToProcess @() `
    -RequiredAssemblies @("Fiddler.dll") `
    -NestedModules @() `
    -Copyright "" `
    -ModuleToProcess "Fiddler.psm1" `
    -TypesToProcess @() `
    -FileList @("Fiddler.psm1","Fiddler.dll")

I think the answer to your question would be to include your file list in the FileList parameter there.

I wrote some of my findings (including links to MS resources) here:

http://amonkeysden.tumblr.com/post/5127684898/powershell-and-fiddler

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文