Powershell模块初始化 [英] Powershell module initialization

查看:363
本文介绍了Powershell模块初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在加载模块时,PowerShell会调用任何初始化代码吗?

Does PowerShell call any initialization code when a module is loaded?

我正在寻找Perl BEGIN块或构造函数之类的东西.

I am looking for something like a Perl BEGIN block, or a constructor.

NEW-MODULE和IMPORT-MODULE都将返回PSCustomObject.我试图将自定义对象封装在模块中,以避免脚本中冗长的代码.在开放代码中测试良好的一种方法是:

Both NEW-MODULE and IMPORT-MODULE will return a PSCustomObject. I am trying to encapsulate a custom object in a module to avoid lengthy code in scripts. One method that tests well in open code is:

$m = new-module -scriptblock {
New-Object PSCustomObject | 
    Add-Member NoteProperty -name person -value Frodo -passthru | 
    Add-Member ScriptMethod Who { $this.person }  -passthru |
    Add-Member ScriptMethod Mod { 
        param($x)
        $this.person = $x
        } -passthru
} -ascustomobject -returnresult

理想情况下,我想将此代码放入模块中并使用类似以下内容:

Ideally I would like to drop this code into a module and use something like:

$MyObj = Import-Module -Name ".\MyPackage" -AsCustomObject

并让MyObj作为对象的句柄,与第一个代码段所提供的一样.

and have MyObj be a handle to an object the same as the first snippet provides.

建议表示赞赏.

推荐答案

目前尚不清楚是要在加载模块时运行初始化代码(例如Perl的BEGIN块)还是要创建自定义类( 构造函数"的含义).

It's not clear if you want to run initialization code when a module is loaded (like Perl's BEGIN block) or if you want to create a custom class (which is what "constructor" suggests).

模块中的初始化代码很容易.导入模块时,将执行未嵌入功能的模块中的任何代码.

Initialization code in a module is easy. Any code in a module not embedded in a function is executed when the module is imported.

PS本身不支持创建自定义类.但是请参阅: http://psclass.codeplex.com/.您还可以编写C#,VBScript等,并使用Add-Type.

Creating a custom class isn't supported natively in PS. But see: http://psclass.codeplex.com/. You can also write C#, VBScript, etc. and use Add-Type.

导入模块无法模拟类,因为您只能使用给定名称的一个模块实例-充其量只有一个Singleton类. (顺便说一句,import-module确实有一个-passthru参数,它将或多或少地使您的最后一行代码正常工作-作为一个单例.您还必须在模块代码中添加export-module -variable * -function *),您可以使用New-模块虽然模拟一个类.您可以将其包装在名为new-myClass的函数中.

Import-module won't work to simulate a class, because you can only have 1 instance of a module with a given name - at best you'd have a singleton class. (BTW, import-module does have a -passthru parameter, which would more or less make your last line of code work - as a singleton. You'd also have to add export-module -variable * -function * to your module code) You could use New-Module to simulate a class though. And you could wrap it in a function named, new-myClass for example.

顺便说一句,如果您使用-ASCustomObject参数,则会得到一个不支持"this"的哈希表(换句话说,作为脚本块的哈希表值没有内置的方式来引用哈希表本身).如果使用不带-AsCustomObject的新模块(并且可能使用工厂函数,例如new-myclass),则可以使用& $myModule $varInMyModule模拟"this.varInMyModule".但是,如果使用Add-Member创建PSCustomObject,则脚本方法可以访问$ this,并且它的行为通常更像是具有属性和方法的典型对象.

BTW, if you use the -ASCustomObject parameter you end up with a hashtable, which doesn't support "this" (in words hash table values that are script blocks don't have a built-in way to refer to the hashtable itself). If you use new-module without -AsCustomObject (and potentially use a factory function, for example new-myclass) then you could simulate "this.varInMyModule" with & $myModule $varInMyModule. However if you create a PSCustomObject, using Add-Member, then script method have access to $this and it in general acts a lot more like a typical object with properties and methods.

这篇关于Powershell模块初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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