RJ96
RJ96

Reputation: 313

Expanding object properties in powershell class

i am creating a PS class and want to expand the properties of a certain object, i did this

Class SchulKlasse 
{
[int]$JahrGang 
[int]$schulerAnzahl
[string]$KlassenLehrer
[string]$schulForm
[string]$Name 
[int]$klassenRaum 
[string]$Gebäude 
[string]$Fächer


Schulklasse ([string]$Name, $Gebäude, $SchulerAnzahl,$Fächer, $JahrGang, $KlassenLehrer, $schulForm, $KlassenRaum)
{
    $this.Name = $Name
    $this.Gebäude = $Gebäude
    $this.schulerAnzahl = $SchulerAnzahl
    $this.Fächer = $Fächer
    $this.JahrGang = $JahrGang
    $this.KlassenLehrer = $KlassenLehrer
    $this.schulForm = $schulForm
    $this.klassenRaum = $KlassenRaum
}


}


$newKlassTest = [SchulKlasse]::new('Erfolg','B20','2015','Mathe,Wirtschaft,Sport','1996','H.Müller','GrundSchule','13')

now based on the users choice i want to expand the $klassenLehrer Object

$newKlassTest
$test = New-Object -TypeName PSObject
$test | Add-Member -MemberType NoteProperty -Name Lehrer -Value $newKlassTest.KlassenLehrer
$test | Add-Member -MemberType NoteProperty -Name Nachname -Value **** 
$test | Add-Member -MemberType NoteProperty -Name Vorname -Value ****
$test | Add-Member -MemberType NoteProperty -Name Kenntnisse -Value "master in it management"
$test | Add-Member -MemberType NoteProperty -Name Faecher -Value "Datenbanken,Vernetzte Systeme"
$test | Add-Member -MemberType NoteProperty -Name Geburtsdatum  -Value 1979
$test | Add-Member -MemberType NoteProperty -Name Verfügbarkeit -Value "Montags bis Donnerstags"

$Anfrage = Read-host "Möchten Sie mehr Informationen über den Klassenlehrer wissen? j,n"
if ($Anfrage -eq 'j')
{
$test
}
else {}

it is working well but it seems very primitive and i don't know if there's a better more practical way of doing it, maybe add it to the class or do it as a method?

if anyone knows please share with me, thank you

Upvotes: 0

Views: 266

Answers (1)

T-Me
T-Me

Reputation: 1884

Currently you are using 2 different kind of objects. $newKlassTest of class [SchulKlasse] and $test which is a PSObject. I would suggest you to create a class [Person] or [Lehrer] and let [Schulklasse ].KlassenLehrer be of class [Lehrer]. The class [Lehrer] schould have the properties you added to $test.
However you current constructor of the class [SchulKlasse] would require the [Lehrer]-Object to exist before the [SchulKlasse].

Upvotes: 1

Related Questions