1

I wanted to define class in PowerShell. Inside that class I wanted to use function defined elsewhere, so that when one asks for that value it is automatically run when needed. Following is an example of this.

class ActiveDirectoryUser {
    $DisplayName
    $LastName
    $FirstName
    $FirstNameNonLatin =  Remove-StringLatinCharacters -String $FirstName 
}

However this doesn't really work. It works in C# so what is the equivalent for this in PowerShell?

I am using this code in a way:

 $user = New-Object ActiveDirectoryUser   
 $user.DisplayName = $u.DisplayName
 $user.LastName = $u.LastName
 $user.FirstName = $u.FirstName

 $user | ft *  # should show all fields including FirstNameNonLatin 

2 Answers 2

2

You need a constructor to set the default value using another property. Ex:

function Remove-StringLatinCharacters ([string]$String) { $string.Substring(0,1) }

class ActiveDirectoryUser {
    [string]$DisplayName
    [string]$LastName
    [string]$FirstName
    [string]$FirstNameNonLatin

    ActiveDirectoryUser ([string]$DisplayName, [string]$LastName, [string]$FirstName) {
        $this.DisplayName = $DisplayName
        $this.LastName = $LastName
        $this.FirstName = $FirstName
        $this.FirstNameNonLatin =  (Remove-StringLatinCharacters -String $FirstName)
    }
}

[ActiveDirectoryUser]::new("Disp","Last","First")

DisplayName LastName FirstName FirstNameNonLatin
----------- -------- --------- -----------------
Disp        Last     First     F 
Sign up to request clarification or add additional context in comments.

2 Comments

Is there a way without constructor? I am using new-object and than adding what is nessecary one by one? I tried to use ( ) as i thought it would solve it but it doesn't do anything (empty string is returned).
There are no getters and setters in classes and you need something to trigger the expression which is what I used a constructor for. Without it, you would have to use a scriptproperty as @gvee shows below (added with add-member) or you could create a hidden property and a public method to retrieve it, but you would have to call $myobj.FirstNameNonLatin() to access it.
1

Give this a whirl

function Get-FormattedFullName {
    Param (
        [string]
        $FirstName
        ,
        [string]
        $LastName
    )

    Process {
        return "{0} {1}" -f $FirstName, $LastName
    }

}

$ActiveDirectoryUser = New-Object -TypeName PSObject
$ActiveDirectoryUser | Add-Member -MemberType NoteProperty   -Name "FirstName" -Value "John"
$ActiveDirectoryUser | Add-Member -MemberType NoteProperty   -Name "LastName"  -Value "Smith"
$ActiveDirectoryUser | Add-Member -MemberType ScriptProperty -Name "FullName"  -Value `
    { #Get
        return Get-FormattedFullName -FirstName $this.FirstName -LastName $this.LastName
    }
`

$ActiveDirectoryUser

Results:

FirstName LastName FullName    
--------- -------- --------    
John      Smith    John Smith 

If we then update the LastName property of the object, the FullName reflects this change too:

$ActiveDirectoryUser.LastName = "CHANGED"

$ActiveDirectoryUser

Results:

FirstName LastName FullName    
--------- -------- --------    
John      CHANGED  John CHANGED

3 Comments

Don't think this is what I need. You are running the method at the assigning phase. Which is not what I need.
@MadBoy it might look like that, but it isn't.... I have updated the code with an example illustrating this.
He adds a property that is calculated every time you call it. That's the difference between a NoteProperty and a ScriptProperty (like @gvee has used)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.