Create HashTable With Multiple Values

Hi,

I think i need a hash table for this?
I would like a script to loop through a hash table and for each line, pass the values to my function.

I have a working solution for 2 columns or a key and a value i believe?:

$accounts = @
item1 = 'string_value1'
item2 = 'string_value2'
item3 = 'string_value3'
}

foreach ($line in $accounts.GetEnumerator()){
Get-MyBalanceFunction -account $($line.Name) -string $($line.Value)
}

My issue is trying to add a second value, I would like the list to have 3 columns which i believe would be a key and 2 values?

Example (which im sure is not how i should do it):

$accounts = @
item1 = 'string_value1','int_1'
item2 = 'string_value2','int_2'
item3 = 'string_value3','int_3'
}

foreach ($line in $accounts.GetEnumerator()){
Get-MyBalanceFunction -account $($line.Name) -string $($line.Value1) -int $($line.Value2)
}

Any help on achieving this would be much appreciated.

Thanks

Jamie

If I got it right you can simply use CSV data …

$Beatles = @’
FirstName,LastName,Age,Sex
John,Lennon,40,male
George,Harrison,58,male
Paul,McCartney,78,male
Ringo,Starr,80,male
'@ |
ConvertFrom-Csv -Delimiter ‘,’

foreach($Beatle in $Beatles){
"This Beatle’s first name is {0}, the last name is {1} " -f $Beatle.FirstName,$Beatle.LastName
}

Hi Olaf,

Thank you for your reply, i was considering a CSV format as had used that in the past for something but wasn’t sure that was the right thing with not actually reading from or outputting to a CSV.

I have used your example script and it works but when i apply it to my script it doesn’t work as the vales are passed to the function as 0 and 1 rather than the variable value i.e. item1 and string_value1

$accounts = @'
account,stringval,intval
item1,string_value1,int_1
item2,string_value2,int_2
item3,string_value3,int_3
'@ | ConvertFrom-Csv -Delimiter ','

foreach ($line in $accounts){
Get-MyBalanceFunction -account {0} -string {1} -f $line.account,$line.stringval
}

Sorry if i have missed something obvious.

Almost … :wink:

foreach ($line in $accounts) {
    Get-MyBalanceFunction -account $line.account -string $line.stringval
}

The -f (format) operator is for strings … but you don’t want strings - you want objects with properties in this case. :wink:

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_operators?view=powershell-7#format-operator–f

https://ss64.com/ps/syntax-f-operator.html

Thank you so much, it is working now

I played with those variables based on a previous script i had written but that uses $_.var which didn’t work, still getting used to powershell :).

Thank you for your help

 

Cheers

Jamie

… difference between foreach and ForEach-Object:wink:

Ah i kind of thought foreach was an alias of foreach-object (and to be fair i try not to use aliases in powershell but i keep seeing foreach) which i can now see they are not exactly the same, thank you for your help.

Having read up on it a little now, i came across another post here which helps too (https://powershell.org/forums/topic/foreach-vs-foreach-object/), thank you.

 

Cheers,

Jamie

Unfortunately it is and I think that wasn’t the best idea for an alias in this case. But it’s actually easy to memorize if it has a pipe in front of it then it is ForEach-Object. :wink:

Cheers for the tip :slight_smile: