简体   繁体   English

PowerShell:如何从模块文件psm1和作为外部ps1文件导出功能

[英]PowerShell: How to export a function from the module file psm1 and as external ps1 file

Im trying to find out how i can both export a module member (function) that is written within the psm1 file and a member function that is written within its own ps1 file. 我试图找出如何既可以导出在psm1文件中编写的模块成员(函数)又可以导出在其自己的ps1文件中编写的成员函数。 But both need to be exported as a member of the same module/project ... 但是两者都需要导出为同一模块/项目的成员...

eg the first function is too small or too simpleton to not have its own file, the second function is an advanced function that does need its own file to keep an overview of the whole shabang ... 例如,第一个函数太小或太简单以至于没有自己的文件,第二个函数是高级函数,它需要自己的文件来保持整个shabang的概览...

Now how would i have exported both and even assign an alias to both cases ... 现在我将如何导出这两种情况,甚至为这两种情况分配别名...

Can anyone explain how i would achieve that, my tries result in errors so far, altho i achieved both seperatly. 谁能解释我将如何实现这一目标,到目前为止,我的尝试导致了错误,尽管我分别实现了这两项目标。

Tnx in advance. 提前Tnx。

Have you tried dot sourcing? 您是否尝试过网点采购?

It won't load the ps1 as part of the module (For that the function would need to be IN the module and not in a separate ps1 file) but it will still load the functions from it. 它不会将ps1作为模块的一部分加载(为此,该功能必须位于模块中,而不是在单独的ps1文件中),但仍会从中加载功能。

# Load the module
Import-Module ".\MyPowershellModule.psm1"

# Load the ps1
. ".\MyPowershellScript.ps1"

# Use a function from ps1
FunctionFromPs1 -ThisParam -ThatParam

# Use a function from module
FunctionFromModule -ThisParam -ThatParam

The only other way i can think of (and i would strongly advise against this) is to dot source the ps1 in your module and load the module 我能想到的唯一其他方法( 强烈建议您这样做)是将ps1点源化到模块中并加载模块

Is there a reason you can't add the function to your module? 您是否有不能将功能添加到模块的原因? Module are supposed to be a large repository of functions (Mine personally is 3k+ lines). 模块应该是功能的大型存储库(我个人认为是3k +行)。 I do agree that having all your functions in one file can get overwhelming but having them in one location beats having 30 ps1 files. 我确实同意,将所有功能都放在一个文件中会让人不知所措,但是将它们放在一个位置会比拥有30 ps1文件要好。

These are some steps I took .... 这些是我采取的步骤。

step 1: Create a psm1 file with 2 simple function then export them both. 步骤1:使用2个简单函数创建一个psm1文件,然后将它们都导出。 This works fine and as expected 这工作正常,并且符合预期

Step 2: Create an external file with the name of the 3rd function. 第2步:使用第3个函数的名称创建一个外部文件。 PowerShell wont see the 3rd function PowerShell将看不到第三功能

Step 3: Ad a dotsourcing line at the top of the module psm1 file. 步骤3:在模块psm1文件的顶部添加一个点采购行。

PowerShell wont see the 3rd function PowerShell将看不到第三功能

Step 4: Create a psd1file and add the 3rd function to 'FunctionsToExport'. 步骤4:创建一个psd1文件,并将第三个函数添加到“ FunctionsToExport”。 Powershell wont see the first two functions Powershell不会看到前两个功能

Step 5: Add Those two functions to 'FunctionsToExport' in the psd1 file PowerShell sees all the functions and everything works fine and as expected 步骤5:将这两个函数添加到psd1文件中的“ FunctionsToExport”中,PowerShell可以看到所有函数,并且一切正常且符合预期

Step 6: Comment out the dotsourcing codeline at the top of psm1 file Function Three dissapears (This is the external function) when clicked Function 1 and 2 within the psm1 file work fine. 步骤6:注释掉psm1文件顶部的dotsourcing代码行。功能单击psm1文件中的功能1和2可以正常工作时,三个消失的地方(这是外部功能)。

Step 7: Add all files to the FileList Property of the psd1 file Same Situation as Step 6 步骤7:将所有文件添加到psd1文件的FileList属性中与步骤6相同的情况

Step 8: Add the psm1 file to the ModuleList property of the psd1 file. 步骤8:将psm1文件添加到psd1文件的ModuleList属性。 Same Situation as Step 6 与步骤6相同

Step 9: Add (external) function 3 to the ScriptsToProcess property of the psd1 file Same Situation as Step 6 步骤9:将(外部)函数3添加到psd1文件的ScriptsToProcess属性中与步骤6相同的情况

After taking these steps i dont see any other way then dotsourcing external ps1 files from within the psm1 file in conjuntion with telling about all of of the functions from within the psd1 file ... In this way all functions will be seen by powershell 在执行了这些步骤之后,我看不到任何其他方法,然后从psm1文件中对外部ps1文件进行点外包,同时讲述psd1文件中的所有功能...这样,powershell将看到所有功能

Alternative to dotsourcing would be using the nested modules property of a manifest 替代点外包的方法是使用清单的嵌套模块属性

According to https://docs.microsoft.com/en-us/powershell/developer/module/how-to-write-a-powershell-module-manifest 根据https://docs.microsoft.com/zh-CN/powershell/developer/module/how-to-write-a-powershell-module-manifest

You can list the relative paths to ps1 and psm1 files in the nested modules property of the psd1 manifest 您可以在psd1清单的nested modules属性中列出ps1和psm1文件的相对路径。

You also need to list all the functions to export in the manifest as well. 您还需要列出清单中要导出的所有功能。 See also Get List Of Functions From Script 另请参阅从脚本获取功能列表

function Get-RunningServices {get-service | where-object {$_.Status -eq "running"}|Select-Object -Property DisplayName, Name| Sort-Object -property DisplayName}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM