在模块中使用方法模拟类 [英] Emulating a class with methods in a module

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

问题描述

我正在使用PowerShell 2.0(无法升级),并且正在编写一系列脚本,这些脚本使用Active Directory中的某些信息.来自C ++之类的OOP语言,我想在PowerShell 2.0中模拟一个类,但我知道它们在5.0中仅具有 class 语句,并且我不想使用C#嵌入类因为我已经有用Powershell编写的函数(将是我的方法).

I am using PowerShell 2.0 (can't upgrade) and I am writing a series of scripts that uses some information from Active Directory. Coming from an OOP languages like C++, I want to emulate a class in PowerShell 2.0, but I know that they only have the class statement in 5.0, and I don't want to use C# to embed classes because I already have a functions (which will be my methods) written in Powershell..

我读了这篇文章: Powershell.创建一个类文件来保存自定义对象?

我能够用"members"创建一个创建PSObject的函数,但不确定如何使它与方法配合使用,以便我可以在脚本中加载该函数以得到更简洁的代码.

And I am able to do a function creating a PSObject with "members", but not sure how to make it work with methods so that I can just load the function in my scripts to have a cleaner code.

这是我到目前为止所拥有的:

This is what I have so far:

function New-User {

    param($user,
         $headers = @("header1","header2","header3","header4") )

    $new_user = New-Object -TypeName PSObject
    foreach ($header in $headers){
        $new_user | Add-Member -membertype NoteProperty -name $header -value $user.$header
    }
   $new_user | Add-Member -membertype NoteProperty -name "othermember" -value ""
   $new_user.PSObject.Typenames.Insert(0,"AD-User")

   # Add methods here! for example:
   $new_user | Add-Member -membertype METHOD -name "othermember" -value     MYDEFINED_FUNCTION($new_user.header1)


   return $new_user

}

如何做到这一点,以便只在脚本中加载此功能?

How I can do this so that I just have this function loaded in my script?

谢谢!

推荐答案

ScriptMethod s可以填补您在这里寻找的空白.假设对象具有firstnamelastname的属性,则可以执行以下操作.

ScriptMethods could fill the gap you are looking for here. Assuming the object has a property for firstname and lastname you could do something like this.

$new_user | Add-Member -membertype ScriptMethod -name samaccountname -value  {$this.firstname.substring(0,1) + $this.LastName}

然后使用返回的对象,您可以调用scriptmethod samaccountname

Then with the returned object you can call the scriptmethod samaccountname

$matt = New-Object -TypeName psobject -Property @{
    firstname = "matt"
    lastname = "bagel"
}

$new = new-user $matt "firstname","lastname"
$new.samaccountname()

哪个将返回"mbagel".在2.0上进行了测试.

Which would return "mbagel". This was tested on 2.0.

因此,如果您想在方法中使用一个函数,只需确保它在调用之前就已获得即可.

So if you wanted to use a function in the method that is just a matter of making sure it is sourced ahead of the time it is called.

function Get-AccoutName{
    param(
        [string]$firstname,
        [string]$lastname
    )

    $firstname.substring(0,1) + $lastname
}
.....
other code you have
.....
$new_user | Add-Member -membertype ScriptMethod -name samaccountname -value  {Get-AccoutName $this.firstname $this.LastName}

如果希望这些功能始终可用,则只需将它们加载到PowerShell配置文件中.

If you want these things available all the time then you just need to load them into a PowerShell profile.

这篇关于在模块中使用方法模拟类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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