Emulating a class with methods in a module

Viewed 184

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..

I read this: Powershell. Create a class file to hold Custom Objects?

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?

Thanks!

1 Answers
Related