Dynamic variables for use outside loop scope

I am a relative novice so please spell it out for me.

I am using the coolOrange PowerVault module to query Autodesk Vault Items. The query returns $results which are potentially 1000’s of Vault Items with properties/values. Each one of these results could be the same user or different user. The ultimate goal is to collect the items per user then pass those objects to an email, and tell them to take action.

I was trying to loop the $results, create a dynamic object for each unique user which would store their $result, then in another loop outside of this one I would loop the collection of $users and deal with the emails. I don’t understand how to do this.

The outcome of this loop should be an object $users = $LastModUserName, "LastModUserName2,…
Then
$LastModUserName1 = $result1, $result2,…

ForEach ($result in $results) {
    If (!(Test-Path variable:global:$result.LastModUserName)) {
        Write-Output $result.LastModUserName
        New-Variable -Name "$result.LastModUserName"
        $result.LastModUserName += $result
    }
}

Take a look at Group-Object to group by username and then you can enumerate through those groups to get other variables. Look at this example:

$objects = @()
$objects += [pscustomobject]@{
    UserName = 'JSmith'
    Action = 'Some Action Message 1'
}
$objects += [pscustomobject]@{
    UserName = 'TCarter'
    Action = 'Some Action Message 1'
}
$objects += [pscustomobject]@{
    UserName = 'FThomps'
    Action = 'Some Action Message 1'
}
$objects += [pscustomobject]@{
    UserName = 'JSmith'
    Action = 'Some Action Message 2'
}
$objects += [pscustomobject]@{
    UserName = 'FThomps'
    Action = 'Some Action Message 2'
}

$groups = $objects | Group-Object -Property UserName

foreach ($group in $groups) {
    
$mail = @"
Hello $($group.Name),

You have $($group.Count) action messages:

$(foreach ($action in $group.Group.Action) {"- {0}`r`n" -f $action})
"@

$mail
}

A group object looks like this:

PS C:\Users\Rob\Desktop\Archive> $groups

Count Name                      Group                                                                                                                                                                                                            
----- ----                      -----                                                                                                                                                                                                            
    2 JSmith                    {@{UserName=JSmith; Action=Some Action Message 1}, @{UserName=JSmith; Action=Some Action Message 2}}                                                                                                             
    1 TCarter                   {@{UserName=TCarter; Action=Some Action Message 1}}                                                                                                                                                              
    2 FThomps                   {@{UserName=FThomps; Action=Some Action Message 1}, @{UserName=FThomps; Action=Some Action Message 2}}                                                                                                           

The mail example looks like:

Hello JSmith,

You have 2 action messages:

- Some Action Message 1
 - Some Action Message 2

Hello TCarter,

You have 1 action messages:

- Some Action Message 1

Hello FThomps,

You have 2 action messages:

- Some Action Message 1
 - Some Action Message 2

Rob, I can see you hard coded the username whereas I have no way of knowing how many usernames would exist. I opted for a variant of your suggestion which appears to work as needed. This is the first time I’ve used the Group-Object method.

I only have one problem. How can I get rid of the single white space in front of $action line2?

$objects = @()
ForEach ($result in $results) {
    $objects += [PSCustomObject]@{
        UserName = $result.LastModUserName
        Action = $result.ItemNum
    }
}

$groups = $objects | Group-Object -Property UserName