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:
$parentObject.Children.Children.Andrew."Birthday Info" --- is there a programmatic way to know to need quotes and add quotes?"
$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?
$parentObject."01/01/1960". --- why can't I dot source the child properties
$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
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}
For some reason when I wrote this Powershell object, the last member does not get displayed in my result:
$resultObject = New-Object -TypeName psobject
$resultObject | Add-Member -MemberType NoteProperty -Name Date -Value $date
$resultObject | Add-Member -MemberType NoteProperty -Name TotalPCs -Value $ad
$resultObject | Add-Member -MemberType NoteProperty -Name UnmanagedTotal -Value $total
$resultObject | Add-Member -MemberType NoteProperty -Name TotalEMEA -Value $adEMEA
$resultObject | Add-Member -MemberType NoteProperty -Name UnmanagedEMEA -Value $totalEMEA
$resultObject | Add-Member -MemberType NoteProperty -Name TotalAFRICA -Value $adAFRICA
$resultObject | Add-Member -MemberType NoteProperty -Name UnmanagedAFRICA -Value $totalAFRICA
$resultObject | Add-Member -MemberType NoteProperty -Name TotalAMERICAS -Value $adAMERICAS
$resultObject | Add-Member -MemberType NoteProperty -Name UnmanagedAMERICAS -Value $totalAMERICAS
$resultObject | Add-Member -MemberType NoteProperty -Name TotalAPAC -Value $adAPAC
$resultObject | Add-Member -MemberType NoteProperty -Name UnmanagedAPAC -Value $totalAPAC
$resultObject | ft -AutoSize | out-file -FilePath "C:\BAC\WSUSScripts\KPI\ManagedComputers.txt" -Append
I have no clue why since every info is retrieved the same way and for the other member it all works. The last member just falls off.
I have simplified the process so that anyone can reproduce it (in my script it is a problem within a workflow, no functions involved, therefore I can`t save these values in variables and then show them by console). The first thing is to use powershell ISE.
function AAA {
$datum1 = New-Object -TypeName PSObject
$datum1 | Add-Member -MemberType NoteProperty -Name ColumnA -Value "AA"
$datum1 | Add-Member -MemberType NoteProperty -Name ColumnB -Value "BB"
return $datum1
}
function BBB {
$datum2 = New-Object -TypeName PSObject
$datum2 | Add-Member -MemberType NoteProperty -Name ColumnA -Value "AA"
$datum2 | Add-Member -MemberType NoteProperty -Name ColumnC -Value "CC"
$datum2 | Add-Member -MemberType NoteProperty -Name ColumnD -Value "DD"
$datum2 | Add-Member -MemberType NoteProperty -Name ColumnE -Value "EE"
$datum2 | Add-Member -MemberType NoteProperty -Name ColumnF -Value "FF"
return $datum2
}
AAA
BBB
Output:
ColumnA ColumnB
------- -------
AA BB
AA
Any idea why this happens?
EDIT:
I have solved this "sh1t" in the following way
function AAA {
$datum1 = New-Object -TypeName PSObject
$datum1 | Add-Member -MemberType NoteProperty -Name Index -Value "1"
$datum1 | Add-Member -MemberType NoteProperty -Name ColumnA -Value "AA"
$datum1 | Add-Member -MemberType NoteProperty -Name ColumnB -Value "BB"
$datum1
}
function BBB {
$datum2 = New-Object -TypeName PSObject
$datum2 | Add-Member -MemberType NoteProperty -Name Index -Value "2"
$datum2 | Add-Member -MemberType NoteProperty -Name ColumnA -Value "AA"
$datum2 | Add-Member -MemberType NoteProperty -Name ColumnC -Value "CC"
$datum2 | Add-Member -MemberType NoteProperty -Name ColumnD -Value "DD"
$datum2 | Add-Member -MemberType NoteProperty -Name ColumnE -Value "EE"
$datum2 | Add-Member -MemberType NoteProperty -Name ColumnF -Value "FF"
$datum2
}
AAA |? {$_.Index -eq "1"} | FT
BBB |? {$_.Index -eq "2"} | FT
Anyway, can someone explain what's going on? I would really like to understand the reason and look for related information
EDIT2: Reply to Palle Due,
it doesn't work at all for my scenario as it adds a bit more complexity.
Out-Host is not an option. Example provided:
workflow Get-Report
{
param ([string[]]$computername)
foreach -Parallel ($computer in $computername) {
sequence {
InlineScript {
$AAA = Invoke-Command -ScriptBlock {
$datum1 = New-Object -TypeName PSObject
$datum1 | Add-Member -MemberType NoteProperty -Name Index -Value "1"
$datum1 | Add-Member -MemberType NoteProperty -Name ColumnA -Value "AA"
$datum1 | Add-Member -MemberType NoteProperty -Name ColumnB -Value "BB"
return $datum1
}
$AAA | Out-Host
$BBB = Invoke-Command -ScriptBlock {
$datum2 = New-Object -TypeName PSObject
$datum2 | Add-Member -MemberType NoteProperty -Name Index -Value "2"
$datum2 | Add-Member -MemberType NoteProperty -Name ColumnA -Value "AA"
$datum2 | Add-Member -MemberType NoteProperty -Name ColumnC -Value "CC"
$datum2 | Add-Member -MemberType NoteProperty -Name ColumnD -Value "DD"
$datum2 | Add-Member -MemberType NoteProperty -Name ColumnE -Value "EE"
$datum2 | Add-Member -MemberType NoteProperty -Name ColumnF -Value "FF"
return $datum2
}
$BBB | Out-Host
} #endinlinescript
} #endsequence
} #endforeach
} #endWF
$results = #()
$results = Get-Report -Verbose -Computer "localhost","localhost"
$results | FT
The easy fix is to pipe the output to Out-Host:
function AAA {
$datum1 = New-Object -TypeName PSObject
$datum1 | Add-Member -MemberType NoteProperty -Name ColumnA -Value "AA"
$datum1 | Add-Member -MemberType NoteProperty -Name ColumnB -Value "BB"
return $datum1
}
function BBB {
$datum2 = New-Object -TypeName PSObject
$datum2 | Add-Member -MemberType NoteProperty -Name ColumnA -Value "AA"
$datum2 | Add-Member -MemberType NoteProperty -Name ColumnC -Value "CC"
$datum2 | Add-Member -MemberType NoteProperty -Name ColumnD -Value "DD"
$datum2 | Add-Member -MemberType NoteProperty -Name ColumnE -Value "EE"
$datum2 | Add-Member -MemberType NoteProperty -Name ColumnF -Value "FF"
return $datum2
}
AAA | Out-Host
BBB | Out-Host
gives the output:
ColumnA ColumnB
------- -------
AA BB
ColumnA : AA
ColumnC : CC
ColumnD : DD
ColumnE : EE
ColumnF : FF
The thing is that powershell likes to output custom objects with four members or less horizontally. With five or more members they are output vertically. And then some strange neatness priciple kicks in: If you output multiple objects in a row, powershell will try to keep the same format. So if you started out horizontally, it will keep going horizontally and only output members with the same name as the first custom object. If you start out vertically it will keep going vertically and output all names.
That's why if you go:
BBB
AAA
you will get
ColumnA : AA
ColumnC : CC
ColumnD : DD
ColumnE : EE
ColumnF : FF
ColumnA : AA
ColumnB : BB
instead.
I haven't been able to find any documentation for this. They're just my own observations.
want to populate the following PSObject so it can have a one:one or one:many relationship between IPs and hostnames.
$hostobj = New-Object PSOBject
$hostobj | Add-Member -MemberType NoteProperty -Name "ip_address" -Value ""
$hostobj | Add-Member -MemberType NoteProperty -Name "hostnames" -Value ""
This gives me the correct headings but when I try add more than one value, it will error one trying to add the repeat values:
$hostobj = New-Object PSOBject
$hostobj | Add-Member -MemberType NoteProperty -Name "ip_address" -Value "1.1.1.1"
$hostobj | Add-Member -MemberType NoteProperty -Name "hostnames" -Value "server1"
$hostobj | Add-Member -MemberType NoteProperty -Name "ip_address" -Value "2.2.2.2"
$hostobj | Add-Member -MemberType NoteProperty -Name "hostnames" -Value "server2"
Why is this?
Can do the one to many bit fairly easily:
$hostobj = New-Object PSOBject
$hostobj | Add-Member -MemberType NoteProperty -Name "ip_address" -Value "1.1.1.1"
$hostobj | Add-Member -MemberType NoteProperty -Name "hostnames" -Value "server1", "server2", "server3"
Either of these is the data set to extra from:
1.1.1.1 server1
2.2.2.2 server2
3.3.3.3 server3
4.5.6.7 server4 server5 server6 server7
8.8.8.8 server8
9.9.9.9 server9
1.1.1.1
server1
2.2.2.2
server2
3.3.3.3
server3
4.5.6.7
server4
server5
server6
server7
8.8.8.8
server8
9.9.9.9
server9
you want to create a list of objects like:
$ArrayList = New-Object System.Collections.ArrayList
$hostobj = New-Object PSOBject
$hostobj | Add-Member -MemberType NoteProperty -Name "ip_address" -Value "1.1.1.1"
$hostobj | Add-Member -MemberType NoteProperty -Name "hostnames" -Value "server1"
$ArrayList.Add($hostobj)
$hostobj = New-Object PSOBject
$hostobj | Add-Member -MemberType NoteProperty -Name "ip_address" -Value "2.2.2.2"
$hostobj | Add-Member -MemberType NoteProperty -Name "hostnames" -Value "server2"
$ArrayList.Add($hostobj)
so that $ArrayList contains all the host objects. and each object has one value for ip_address and hostnames.
If this does not help you please specify what you expect/what exactly you are trying to do/work with.
I have a problem :(
I have many errors when I add New object with the same name of property which previously Object .
"Add-Member : Cannot add a member with the name "ServerName" because a
member with that name already exists. To overwrite the member anyway,
add the Force par ameter to your command."
it works but with errors;)
Have someone a solution for me ??
Below example code :
$OutputObj = #()
$OutputObj += New-Object -TypeName PSobject
$OutputObj | Add-Member -MemberType NoteProperty -Name ServerName -Value "server1"
$OutputObj | Add-Member -MemberType NoteProperty -Name Status -Value "online"
$OutputObj | Add-Member -MemberType NoteProperty -Name Uptime -Value "01:20"
$OutputObj += New-Object -TypeName PSobject
$OutputObj | Add-Member -MemberType NoteProperty -Name ServerName -Value "server2"
$OutputObj | Add-Member -MemberType NoteProperty -Name Status -Value "online"
$OutputObj | Add-Member -MemberType NoteProperty -Name Uptime -Value "01340"
You are adding the member on the array, not on the object. Try the following:
$OutputObj = #()
$NewObj = New-Object -TypeName PSobject
$NewObj | Add-Member -MemberType NoteProperty -Name ServerName -Value "server1"
$NewObj | Add-Member -MemberType NoteProperty -Name Status -Value "online"
$NewObj | Add-Member -MemberType NoteProperty -Name Uptime -Value "01:20"
$OutputObj += $NewObj
$NewObj = New-Object -TypeName PSobject
$NewObj | Add-Member -MemberType NoteProperty -Name ServerName -Value "server2"
$NewObj | Add-Member -MemberType NoteProperty -Name Status -Value "online"
$NewObj | Add-Member -MemberType NoteProperty -Name Uptime -Value "01340"
$OutputObj += $NewObj
Alernatively, I would do the following:
$OutputObj = #()
$OutputObj += New-Object -TypeName PSobject -Property #{
ServerName = "server1"
Status = "Online"
Uptime = "01:20"
}
$OutputObj += New-Object -TypeName PSobject -Property #{
ServerName = "server2"
Status = "Online"
Uptime = "01340"
}
Since Add-Member destroys and generates a new object each time it is used.