Home > other >  Questions about Powershell objects, property names, multiple property values with the same name and
Questions about Powershell objects, property names, multiple property values with the same name and

Time:06-18

There is code at the bottom of this to help make it easy to reproduce what I'm asking in my questions. My questions are:

  1. $parentObject.Children.Children.Andrew."Birthday Info" --- is there a programmatic way to know to need quotes and add quotes?"

  2. $parentObject.Children.Robert.Children.Peter."Favorite Foods" --- I understand it creates a property name conflict if I try to add "Name" with a different value more than once, how can I add "Name" with a different food value multiple times? Should it be an array that I keep adding to and replacing/updating?

  3. $parentObject."01/01/1960". --- why can't I dot source the child properties

  4. $parentObject.Children.Robert. --- why can't I dot source the child properties, the 3rd level seems to be the limit?

$parentObject.01/01/1960; --- fails, returns a "0"

$parentObject."01/01/1960"; --- returns property names "Location" and "Years Together"

$parentObject.{01/01/1960}; --- returns property names "Location" and "Years Together"

$parentObject."01/01/1960". --- does not dot source

$parentObject.{01/01/1960}. --- does not dot source

$parentObject."01/01/1960".Location; --- returns property value "Hawaii"

$parentObject.{01/01/1960}.Location; --- returns property value "Hawaii"

$parentObject."01/01/1960"."Years Together"; --- returns property value "72"

$parentObject.{01/01/1960}.{Years Together}; --- returns property value "72"

$parentObject.anniversary - 01/01/1960; --- fails, performs a math equation

$parentObject."anniversary - 01/01/1960"; --- returns property values "Hawaii" and "62"

$parentObject.{anniversary - 01/01/1960}; --- returns property values "Hawaii" and "62"

$parentObject."anniversary - 01/01/1960". --- does not dot source

$parentObject.{anniversary - 01/01/1960}. --- does not dot source

Full code:

#Region Instantiate

    $foodsObject = New-Object -TypeName psobject;
    $birthDayObject = New-Object -TypeName psobject;
    $numberOfBirthdaysObject = New-Object -TypeName psobject;
    $grandChildObject = New-Object -TypeName psobject;
    $childObject = New-Object -TypeName psobject;
    $parentObject = New-Object -TypeName psobject;
    $anniversaryObject = New-Object -TypeName psobject;

#EndRegion /Instantiate

#Region Peter

    $numberOfBirthdaysObject = New-Object -TypeName psobject;
    $numberOfBirthdaysObject | Add-Member -MemberType NoteProperty -Name "Count" -Value "22";
    $birthDateObject = New-Object -TypeName psobject;
    $birthDateObject | Add-Member -MemberType NoteProperty -Name "Date" -Value "01/01/2000";
    $birthDateObject | Add-Member -MemberType NoteProperty -Name "Birthday Count" -Value $numberOfBirthdaysObject;

    $birthDayObject = New-Object -TypeName psobject;
    $birthDayObject | Add-Member -MemberType NoteProperty -Name "Birthday Info" -Value $birthDateObject;

    $foodsObject = New-Object -TypeName psobject;
    $foodsObject | Add-Member -MemberType NoteProperty -Name "Spaghetti" -Value "Spaghetti";
    $foodsObject | Add-Member -MemberType NoteProperty -Name "Pizza" -Value "Pizza";
    $foodsObject | Add-Member -MemberType NoteProperty -Name "Ice cream" -Value "Ice cream";

    $personalInformationObject = New-Object -TypeName psobject;
    $personalInformationObject | Add-Member -MemberType NoteProperty -Name "Birthday" -Value $birthDayObject;
    $personalInformationObject | Add-Member -MemberType NoteProperty -Name "Favorite Color" -Value "Red";
    $personalInformationObject | Add-Member -MemberType NoteProperty -Name "Favorite Foods" -Value $foodsObject;
    $grandChildObject | Add-Member -MemberType NoteProperty -Name "Peter" -Value $personalInformationObject;

#EndRegion /Peter


#Region Andrew

    $numberOfBirthdaysObject = New-Object -TypeName psobject;
    $numberOfBirthdaysObject | Add-Member -MemberType NoteProperty -Name "Count" -Value "20";
    $birthDateObject = New-Object -TypeName psobject;
    $birthDateObject | Add-Member -MemberType NoteProperty -Name "Date" -Value "01/01/2002";
    $birthDateObject | Add-Member -MemberType NoteProperty -Name "Birthday Count" -Value $numberOfBirthdaysObject;

    $birthDayObject = New-Object -TypeName psobject;
    $birthDayObject | Add-Member -MemberType NoteProperty -Name "Birthday Info" -Value $birthDateObject;

    $foodsObject = New-Object -TypeName psobject;
    $foodsObject | Add-Member -MemberType NoteProperty -Name "Big Mac" -Value "Big Mac";
    $foodsObject | Add-Member -MemberType NoteProperty -Name "Soup" -Value "Soup";
    $foodsObject | Add-Member -MemberType NoteProperty -Name "Cheese cake" -Value "Cheese cake";

    $personalInformationObject = New-Object -TypeName psobject;
    $personalInformationObject | Add-Member -MemberType NoteProperty -Name "Birthday" -Value $birthDayObject;
    $personalInformationObject | Add-Member -MemberType NoteProperty -Name "Favorite Color" -Value "Green";
    $personalInformationObject | Add-Member -MemberType NoteProperty -Name "Favorite Foods" -Value $foodsObject;

    $grandChildObject | Add-Member -MemberType NoteProperty -Name "Andrew" -Value $personalInformationObject;

#EndRegion /Andrew

#Region Robert

    $numberOfBirthdaysObject = New-Object -TypeName psobject;
    $numberOfBirthdaysObject | Add-Member -MemberType NoteProperty -Name "Count" -Value "47";
    $birthDateObject = New-Object -TypeName psobject;
    $birthDateObject | Add-Member -MemberType NoteProperty -Name "Date" -Value "01/01/1975";
    $birthDateObject | Add-Member -MemberType NoteProperty -Name "Birthday Count" -Value $numberOfBirthdaysObject;

    $birthDayObject = New-Object -TypeName psobject;
    $birthDayObject | Add-Member -MemberType NoteProperty -Name "Birthday Info" -Value $birthDateObject;

    $foodsObject = New-Object -TypeName psobject;
    $foodsObject | Add-Member -MemberType NoteProperty -Name "Burger King" -Value "Burger King";
    $foodsObject | Add-Member -MemberType NoteProperty -Name "Cheese Pizza" -Value "Cheese Pizza";
    $foodsObject | Add-Member -MemberType NoteProperty -Name "Beer" -Value "Beer";

    $personalInformationObject = New-Object -TypeName psobject;
    $personalInformationObject | Add-Member -MemberType NoteProperty -Name "Birthday" -Value $birthDayObject;
    $personalInformationObject | Add-Member -MemberType NoteProperty -Name "Favorite Color" -Value "Maroon";
    $personalInformationObject | Add-Member -MemberType NoteProperty -Name "Favorite Foods" -Value $foodsObject;
    
    $personalInformationObject | Add-Member -MemberType NoteProperty -Name "Children" -Value $grandChildObject;
    $childObject | Add-Member -MemberType NoteProperty -Name "Robert" -Value $personalInformationObject;

#EndRegion /Robert

$grandChildObject = New-Object psobject;

#Region Lisa

    $numberOfBirthdaysObject = New-Object -TypeName psobject;
    $numberOfBirthdaysObject | Add-Member -MemberType NoteProperty -Name "Count" -Value "25";
    $birthDateObject = New-Object -TypeName psobject;
    $birthDateObject | Add-Member -MemberType NoteProperty -Name "Date" -Value "01/01/1997";
    $birthDateObject | Add-Member -MemberType NoteProperty -Name "Birthday Count" -Value $numberOfBirthdaysObject;

    $birthDayObject = New-Object -TypeName psobject;
    $birthDayObject | Add-Member -MemberType NoteProperty -Name "Birthday Info" -Value $birthDateObject;

    $foodsObject = New-Object -TypeName psobject;
    $foodsObject | Add-Member -MemberType NoteProperty -Name "Burgers" -Value "Burgers";
    $foodsObject | Add-Member -MemberType NoteProperty -Name "Rice" -Value "Rice";
    $foodsObject | Add-Member -MemberType NoteProperty -Name "Cherry snowball" -Value "Cherry snowball";

    $personalInformationObject = New-Object -TypeName psobject;
    $personalInformationObject | Add-Member -MemberType NoteProperty -Name "Birthday" -Value $birthDayObject;
    $personalInformationObject | Add-Member -MemberType NoteProperty -Name "Favorite Color" -Value "Purple";
    $personalInformationObject | Add-Member -MemberType NoteProperty -Name "Favorite Foods" -Value $foodsObject;

    $grandChildObject | Add-Member -MemberType NoteProperty -Name "Lisa" -Value $personalInformationObject;

#EndRegion /Lisa

#Region Thomas

    $numberOfBirthdaysObject = New-Object -TypeName psobject;
    $numberOfBirthdaysObject | Add-Member -MemberType NoteProperty -Name "Count" -Value "44";
    $birthDateObject = New-Object -TypeName psobject;
    $birthDateObject | Add-Member -MemberType NoteProperty -Name "Date" -Value "01/01/1978";
    $birthDateObject | Add-Member -MemberType NoteProperty -Name "Birthday Count" -Value $numberOfBirthdaysObject;

    $birthDayObject = New-Object -TypeName psobject;
    $birthDayObject | Add-Member -MemberType NoteProperty -Name "Birthday Info" -Value $birthDateObject;

    $foodsObject = New-Object -TypeName psobject;
    $foodsObject | Add-Member -MemberType NoteProperty -Name "Chicken" -Value "Chicken";
    $foodsObject | Add-Member -MemberType NoteProperty -Name "Pit beef" -Value "Pit beef";
    $foodsObject | Add-Member -MemberType NoteProperty -Name "Coke float" -Value "Coke float";

    $personalInformationObject = New-Object -TypeName psobject;
    $personalInformationObject | Add-Member -MemberType NoteProperty -Name "Birthday" -Value $birthDayObject;
    $personalInformationObject | Add-Member -MemberType NoteProperty -Name "Favorite Color" -Value "Yellow";
    $personalInformationObject | Add-Member -MemberType NoteProperty -Name "Favorite Foods" -Value $foodsObject;

    $personalInformationObject | Add-Member -MemberType NoteProperty -Name "Children" -Value $grandChildObject;
    $childObject | Add-Member -MemberType NoteProperty -Name "Thomas" -Value $personalInformationObject;
        
#EndRegion /Thomas

#Region Jason and Pamela

    #Region Jason
        
        $numberOfBirthdaysObject = New-Object -TypeName psobject;
        $numberOfBirthdaysObject | Add-Member -MemberType NoteProperty -Name "Count" -Value "80";
        $birthDateObject = New-Object -TypeName psobject;
        $birthDateObject | Add-Member -MemberType NoteProperty -Name "Date" -Value "01/01/1942";
        $birthDateObject | Add-Member -MemberType NoteProperty -Name "Birthday Count" -Value $numberOfBirthdaysObject;

        $birthDayObject = New-Object -TypeName psobject;
        $birthDayObject | Add-Member -MemberType NoteProperty -Name "Birthday Info" -Value $birthDateObject;

        $foodsObject = New-Object -TypeName psobject;
        $foodsObject | Add-Member -MemberType NoteProperty -Name "Grits" -Value "Grits";
        $foodsObject | Add-Member -MemberType NoteProperty -Name "Burgers" -Value "Burgers";
        $foodsObject | Add-Member -MemberType NoteProperty -Name "Malt drink" -Value "Malt drink";

        $personalInformationObject = New-Object -TypeName psobject;
        $personalInformationObject | Add-Member -MemberType NoteProperty -Name "Birthday" -Value $birthDayObject;
        $personalInformationObject | Add-Member -MemberType NoteProperty -Name "Favorite Color" -Value "Brown";
        $personalInformationObject | Add-Member -MemberType NoteProperty -Name "Favorite Foods" -Value $foodsObject;
        
        $parentObject | Add-Member -MemberType NoteProperty -Name "Jason" -Value $personalInformationObject;
        
    #EndRegion /Jason
    
    #Region Pamela
    
        $numberOfBirthdaysObject = New-Object -TypeName psobject;
        $numberOfBirthdaysObject | Add-Member -MemberType NoteProperty -Name "Count" -Value "78";
        $birthDateObject = New-Object -TypeName psobject;
        $birthDateObject | Add-Member -MemberType NoteProperty -Name "Date" -Value "01/01/1944";
        $birthDateObject | Add-Member -MemberType NoteProperty -Name "Birthday Count" -Value $numberOfBirthdaysObject;

        $birthDayObject = New-Object -TypeName psobject;
        $birthDayObject | Add-Member -MemberType NoteProperty -Name "Birthday Info" -Value $birthDateObject;

        $foodsObject = New-Object -TypeName psobject;
        $foodsObject | Add-Member -MemberType NoteProperty -Name "Rice" -Value "Rice";
        $foodsObject | Add-Member -MemberType NoteProperty -Name "Cheese" -Value "Cheese";
        $foodsObject | Add-Member -MemberType NoteProperty -Name "Popsicle" -Value "Popsicle";

        $personalInformationObject = New-Object -TypeName psobject;
        $personalInformationObject | Add-Member -MemberType NoteProperty -Name "Birthday" -Value $birthDayObject;
        $personalInformationObject | Add-Member -MemberType NoteProperty -Name "Favorite Color" -Value "Pink";
        $personalInformationObject | Add-Member -MemberType NoteProperty -Name "Favorite Foods" -Value $foodsObject;
        
        $parentObject | Add-Member -MemberType NoteProperty -Name "Pamela" -Value $personalInformationObject;
    
    #EndRegion /Pamela
    
    $anniversaryObject | Add-Member -MemberType NoteProperty -Name "Location" -Value "Hawaii";
    $anniversaryObject | Add-Member -MemberType NoteProperty -Name "Years Together" -Value "62";

    if ($parentObject.Children -eq $null)
    {
        $parentObject | Add-Member -MemberType NoteProperty -Name "Father" -Value "Jason";
        $parentObject | Add-Member -MemberType NoteProperty -Name "Mother" -Value "Pamela";
        $parentObject | Add-Member -MemberType NoteProperty -Name "Children" -Value $childObject;
        $parentObject | Add-Member -MemberType NoteProperty -Name "01/01/1960" -Value $anniversaryObject;
        $parentObject | Add-Member -MemberType NoteProperty -Name "anniversary - 01/01/1960" -Value $anniversaryObject;
    }
    else
    {
        $parentObject | Add-Member -MemberType NoteProperty -Name "Father" -Value "Jason";
        $parentObject | Add-Member -MemberType NoteProperty -Name "Mother" -Value "Pamela";
        $parentObject.Children = $childObject;
        $parentObject | Add-Member -MemberType NoteProperty -Name "01/01/1950" -Value $anniversaryObject;
        $parentObject | Add-Member -MemberType NoteProperty -Name "anniversary - 01/01/1950" -Value $anniversaryObject;
    }

#EndRegion Jason and Pamela

CodePudding user response:

I think your elemental object should be a single person with the desired properties, including refences to child objects. Then define a marriage which references the individual people and stores the date & location.

  • Best to just avoid spaces in property names
  • Use tab completion to explore what's available through dot notation
  • FavFoods is created as an array by splitting a string that uses an arbitrary separator ( )
  • ScriptProperties are used to keep time-dependent values current

Play with the objects generated by this code:

@'
Name,Birthday,FavColor,FavFoods
Jason,01/01/1942,Brown,Grits Burgers Malt Drink
Pamela,01/01/1944,Pink,Rice Cheese Popsicle
Robert,01/01/1975,Maroon,Burger King Cheese Pizza Beer
Thomas,01/01/1978,Yellow,Chicken Pit Beef Coke float
Peter,1/1/2000,Red,Spaghetti Pizza Ice cream
Andrew,1/1/2002,Green,Big Mac Soup Cheesecake
'@ | convertFrom-Csv | ForEach {[PSCustomObject]@{
    'Name'  = $_.Name
    'Birthday' = $_.Birthday
    'FavColor' = $_.FavColor
    'FavFoods' = $_.FavFoods.Split(' ')
    'Children' = @{}
} | Add-Member -MemberType ScriptProperty -Name Age -Value { ([DateTime]((Get-Date) - [DateTime]($This.Birthday)).Ticks).Year - 1 } -PassTHru
} | FOrEach { $hash = @{} } {
    $hash.Add($_.Name,$_)
} { $hash } | sv People

'Jason' , 'Pamela' | ForEach {
    $Parent = $_
    'Robert' , 'THomas' | ForEach {
         $People[$Parent].Children.Add( $_ , $People[$_] )
    }
}

'Peter' , 'Andrew' | ForEach {
    $People.Robert.Children.Add( $_ , $People[$_] )
}

$Marriage = [PSCustomObject]@{
    Spouses = @( $People['Jason'],  $People['Pamela'] )
    Date    = '1/1/1960'
    Location = 'Hawaii'
    Children = $People['Jason'].Children.Keys   $People['Pamela'].Children.Keys | select -Unique | ForEach {$hash = @{} } { $hash.Add($_ , $People[$_] ) } { $hash }
}

$Marriage | Add-Member -MemberType ScriptProperty -Name 'YearsTogether' -Value { ([DateTime]((Get-Date) - [DateTime]($This.Date)).Ticks).Year - 1 }

Samples:

PS C:\> $People.GetEnumerator() | ForEach {  $_.value }


Name     : Jason
Birthday : 01/01/1942
FavColor : Brown
FavFoods : {Grits, Burgers, Malt Drink}
Children : {THomas, Robert}
Age      : 80

Name     : Robert
Birthday : 01/01/1975
FavColor : Maroon
FavFoods : {Burger King, Cheese Pizza, Beer}
Children : {Peter, Andrew}
Age      : 47

Name     : Pamela
Birthday : 01/01/1944
FavColor : Pink
FavFoods : {Rice, Cheese, Popsicle}
Children : {THomas, Robert}
Age      : 78

Name     : Thomas
Birthday : 01/01/1978
FavColor : Yellow
FavFoods : {Chicken, Pit Beef, Coke float}
Children : {}
Age      : 44

Name     : Peter
Birthday : 1/1/2000
FavColor : Red
FavFoods : {Spaghetti, Pizza, Ice cream}
Children : {}
Age      : 22

Name     : Andrew
Birthday : 1/1/2002
FavColor : Green
FavFoods : {Big Mac, Soup, Cheesecake}
Children : {}
Age      : 20



PS C:\>>$Marriage.Children

Name                           Value
----                           -----
THomas                         @{Name=Thomas; Birthday=01/01/1978; FavColor=Yellow; F...
Robert                         @{Name=Robert; Birthday=01/01/1975; FavColor=Maroon; F...


PS C:\>>$Marriage.Children.Robert.Children

Name                           Value
----                           -----
Peter                          @{Name=Peter; Birthday=1/1/2000; FavColor=Red; FavFood...
Andrew                         @{Name=Andrew; Birthday=1/1/2002; FavColor=Green; FavF...


PS C:\>>$Marriage.Children.Robert.Children.Values | select Name, FavFoods

Name   FavFoods
----   --------
Peter  {Spaghetti, Pizza, Ice cream}
Andrew {Big Mac, Soup, Cheesecake}


  • Related