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
Olaf
April 24, 2020, 4:39am
2
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.
Olaf
April 24, 2020, 5:36am
4
Almost …
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.
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
Olaf
April 24, 2020, 6:19am
6
… difference between foreach and ForEach-Object …
… but that uses $_.var which didn’t work, still getting used to powershell :).
… difference between foreach and ForEach-Object … 😉
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
Olaf
April 24, 2020, 6:40am
8
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.