How to holding foreach in hashtable

i write a script and i need to add the each object in hashtable ,but when i try put foreach in the hashtable ,it give me error

 [CmdletBinding()]
param (
    [Parameter(Mandatory)]
    [string]
    $Member
    ,
# Parameter help description
[Parameter(Mandatory)]
[psobject]
$Variable
)
class Device {
}
$addmember = [Device]::new()
## fist member
##id  name sex 
$attribute= ($Variable|Get-Member|Where-Object -Property MemberType -Like "*noteproperty*"|Select-Object -Property Name).name

$variable | ForEach-Object {
  $addmember| Add-Member -NotePropertyMembers @{$_.$Member =  @{
    foreach ($value in $attribute) {
$value = $_.value 
    }
}
  }
}
$addmember

how can i add up the hashtable like this with foreach so it function like this

  $addmember| Add-Member -NotePropertyMembers @{$_.$Member =  @{

name = $_.name 
id= $_.name 
pl=$_.pl
acc= $_.acc
}
  }

Could you try to provide an minimal executable example? … including sample data if possible?

This might help already :

If you get errors you should share them here completely (formatted as code please).

How about using a [PSCustomObject] instead to collect your objects?

the script was trying to convert the psobject into psobject with hashtable inside

5-toubun no Hanayome ∬                                                                                       : {my_status, series_title}
Akagami no Shirayuki-hime                                                                                    : {my_status, series_title}
Akame ga Kill!                                                                                               : {my_status, series_title}
Amagi Brilliant Park                                                                                         : {my_status, series_title}
Angel Beats!                                                                                                 : {my_status, series_title}
Ano Hi Mita Hana no Namae wo Bokutachi wa Mada Shiranai.                                                     : {my_status, series_title}
Ansatsu Kyoushitsu                                                                                           : {my_status, series_title}
Arakawa Under the Bridge                                                                                     : {my_status, series_title}
Aria the Animation                                                                                           : {my_status, series_title}
Baka to Test to Shoukanjuu                                                                                   : {my_status, series_title}
Bakemonogatari                                                                                               : {my_status, series_title}
Black Bullet                                                                                                 : {my_status, series_title}
Bocchi the Rock!                                                                                             : {my_status, series_title}
Boku dake ga Inai Machi                                                                                      : {my_status, series_title}

get the member of the psobject :

$attribute= ($Variable|Get-Member|Where-Object -Property MemberType -Like "*noteproperty*"|Select-Object -Property Name).name

find the psobject’s object and add it into hashtable

$variable | ForEach-Object {
  $addmember| Add-Member -NotePropertyMembers @{$_.$Member =  @{
    foreach ($value in $attribute) {
    $value = $_.value
    }
}
  }
}

the following code will add thehashtablet in each objec ,but hashtable can’t handle foreach inside ,when i try put foreach in the hashtable ,it give me an error
image

Sorry, I still didn’t get it.

Did you read all the help topics I linked in my last reply?

Are you really sure it shouldn’t be $_.Member instead of $_.$Member?? :thinking:

This looks wrong to me. The input array is $attribute (a better name would be $attributeList IMO :wink: ) and the loop variable you create is $value.

But then you overwrite this loop variable $value with the pipeline variable $_.value from the outer Foreach-Object loop?!?! :thinking: :thinking:

I’ll ask again: Could you create a minimal reproducable example we can run as it is to reproduce your issue? It should contain usable input data (maybe some CSV data), your code logic and the expected output.

Thanks in advance.

And please … do not post pictures of code or error messages. Instead post the plain text formatted as code! :point_up:t3:

Seeing this:

I’d like to recommend using the VSCode extension Error Lens. It’s going to help you finding the cause of errors:

this is the example code i wrote before

[CmdletBinding()]
param (
    [Parameter(Mandatory)]
    [string]
    $Member

    ,
# Parameter help description
[Parameter(Mandatory)]
[psobject]
$Variable

)



class Device {

}

$addmember = [Device]::new()




## fist member

##id  name sex 

$attributelist= ($Variable|Get-Member|Where-Object -Property MemberType -Like "*noteproperty*"|Select-Object -Property Name).name



$variable | ForEach-Object {




  $addmember| Add-Member -NotePropertyMembers @{$_.$Member =  @( 
    foreach ($attribute in $attributelist) {
     $_.$attribute
    }


  )

  }
}

$addmember

Get-Content .\demo.csv

id,sex,name
1,male,joy
2,male,peter
3,male,mike
4,female,amy
5,female,julia

$list = Import-Csv .\demo.CSV

.\convertto-attribute.ps1 -Variable $list -Member id



1 : {1, joy, male}
2 : {2, peter, male}
3 : {3, mike, male}
4 : {4, amy, female}
5 : {5, julia, female}

.\convertto-attribute.ps1 -Variable $list -Member name



joy   : {1, joy, male}
peter : {2, peter, male}
mike  : {3, mike, male}
amy   : {4, amy, female}
julia : {5, julia, female}



this would work well when i add the value ,but when i trying to add up the hashtable

$variable | ForEach-Object {




  $addmember| Add-Member -NotePropertyMembers @{$_.$Member =  @{ 
    foreach ($attribute in $attributelist) {
     $attribute= $_.$attribute
    }


  }

  }
}

it gives me error

At C:\Users\Administrator\convertto-attribute.ps1:40 char:12
+     foreach ($attribute in $attributelist) {
+            ~
Missing '=' operator after key in hash literal.
At C:\Users\Administrator\convertto-attribute.ps1:34 char:28
+ $variable | ForEach-Object {
+                            ~
Missing closing '}' in statement block or type definition.
    + CategoryInfo          : ParserError: (:) [], ParseException
    + FullyQualifiedErrorId : MissingEqualsInHashLiteral

i am sorry i have mistype the part

    foreach ($value in $attribute) {
    $value = $_.value
    }

it should be

    foreach ($value in $attribute) {
    $value = $_.$value
    }

Instead of posting a new reply you should go back, edit your original post again and fix it there!! Your code is confusing enough without the need to piece it together from different posts. :man_shrugging:t3:

Please keep in mind that we cannot see your screen and we cannot read your mind. :man_shrugging:t3:

Are there 2 different input files? :thinking: :question:

i am sorry ,no there is only one file that is input

Am I getting this right?

You want to get from this input data…

$InputCSVData = 
ConvertFrom-Csv -InputObject @'
id,sex,name
1,male,joy
2,male,peter
3,male,mike
4,female,amy
5,female,julia
'@ 

To this hashtable?

joy   : {1, joy, male}
peter : {2, peter, male}
mike  : {3, mike, male}
amy   : {4, amy, female}
julia : {5, julia, female}

:question:

If it’s that what you want you’re overcomplicating this really a lot. All you need would be this:

$Hashtable =
$InputCSVData | 
    Group-Object -AsHashTable -Property Name
$Hashtable

The output looks like this:

Name                           Value
----                           -----
julia                          {@{id=5; sex=female; name=julia}}
joy                            {@{id=1; sex=male; name=joy}}
amy                            {@{id=4; sex=female; name=amy}}
peter                          {@{id=2; sex=male; name=peter}}
mike                           {@{id=3; sex=male; name=mike}}

If it’s not this what you want you may explain what it is actually what you’re trying to do. Not the way you think you have to go to accomplish it.

1 Like

my script is the new class and not hashtable, my script convert each object into a member

import-csv c:\path\to\demo.csv

$attributeobject = .\convertto-attribute.ps1 -Variable $list  -Member name |Get-Member

[string]$attributeobject


   TypeName: Device

Name        MemberType   Definition                    
----        ----------   ----------                    
Equals      Method       bool Equals(System.Object obj)
GetHashCode Method       int GetHashCode()             
GetType     Method       type GetType()                
ToString    Method       string ToString()             
amy         NoteProperty Object[] amy=System.Object[]  
joy         NoteProperty Object[] joy=System.Object[]  
julia       NoteProperty Object[] julia=System.Object[]
mike        NoteProperty Object[] mike=System.Object[] 
peter       NoteProperty Object[] peter=System.Object[]



bool Equals(System.Object obj) int GetHashCode() type GetType() string ToString() Object[] amy=System.Object[] Object[] joy=System.Object[] Object[] julia=System.Object[] Object[] mike=System.Object[] Object[] peter=System.Object[]

this is class i try to convert from psobject ,i can change the attribute data in powershell easily

$attributeobject = .\convertto-attribute.ps1 -Variable $list  -Member name 

$attributeobject.julia

$attributeobject.julia =@(5,"julia","female","tall")

$attributeobject.julia


5
julia
female



5
julia
female
tall

but yours is also good enough for me thanks! i will try yours for my script

Since I don’t know what you’re actually trying to do it’s fine with me when you’re satisfied. :man_shrugging:t3: :wink: :love_you_gesture:t3:
I just still think you are making this much harder than needed. :smirk: