Powershell: dynamic variables within foreach

0

0

I am trying to do some math for a dynamic list of users that I will use to run a search and count some results anonymously, then add to a running total per user. So, for instance:

$users = Get-ADUser -filter blah
[int]$usercount = $users.count

for ($z=1; $z -le $usercount; $z++) {
    **** create variable here - $user$z ***
}

When the variable is created, I need it available for further loops where I will add a count to the number already stored in the variable.

And no, I can't use the $user variable, because it must persist after this foreach loop ends.

So, the question is, how to I generate that incrementing variable not knowing the limit of the count of possible objects?

---EDIT---

Adding an easy example of what I am talking about...

After feedback, I am looking at hashtables, but still can't figure out how to reference.

Imagine a dice game between a dynamic list of people with multiple rounds. I want to increment per round their total. My problem is the last line where I try to update the total with the roll. How do I reference the hashtable value?

[CmdletBinding()]

param (
    [parameter(Mandatory=$false)][ValidateRange(1, [int32]::MaxValue)][int]$rounds = "15",
    [parameter(Mandatory=$false)][ValidateRange(1, [int32]::MaxValue)][int]$players = "2"
)

$ptotal = [ordered]@{}
for ($w=1; $w -le $players; $w++) {
    $ptotal.add("player$w", 0)
}

for ($z=1; $z -le $rounds; $z++) {
    Write-Host Round $z

    for ($y=1; $y -le $players; $y++) {

        $roll = (1..6 | get-random) + (1..6 | get-random)
        $ptotal.player$y = $ptotal.player$y + $roll
    }
}

Steven

Posted 2018-11-07T00:14:23.147

Reputation: 3

1There's no need to declare the var beforehand in PS. But you should also research variable scope, which is going to cause problems with the way you're going right now. – music2myear – 2018-11-07T00:18:29.903

I actually do need to declare beforehand, because I will have multiple loops later that will add, so I cannot just use a variable within the loop. Also understand the scope concern, and I will deal with that, I am just looking for how to create the variable in a way that I can read it later... – Steven – 2018-11-07T00:25:47.263

So long as you continue to reference the vars using $Script:var_name you can manipulate them throughout the script, in any loop or function. If this is not what you are asking, perhaps you can clarify? – music2myear – 2018-11-07T00:35:36.213

2You can create variables with New-Variable cmdlet. As for loop does not introduce new scope, them will be available in following loops in current scope by default. You can specify desired scope as parameter to New-Variable cmdlet as well. But, unless you can provide good justification for using variables with dynamic names, you should consider using array instead. – user364455 – 2018-11-07T00:46:20.157

Answers

0

in your example it would be $ptotal["player$y"] += $roll

You can use Hashtables both like associative arrays and objects. Very good reading about them is https://kevinmarquette.github.io/2016-11-06-powershell-hashtable-everything-you-wanted-to-know-about/ (on top of official documentation, of course)

maoizm

Posted 2018-11-07T00:14:23.147

Reputation: 739

@Steven you are welcome. You can use "Vote up" as an expression of gratitude ;) – maoizm – 2018-11-07T15:35:22.060

1@maoizm "Vote up" require 15 points, but OP only have 3. – user364455 – 2018-11-07T18:33:50.523