Adding variable cotaining computername in foreach loop in powershell

Saeed Ahmad 21 Reputation points
2021-07-27T10:38:01.297+00:00

I need to have computer name variable add in output to foreachloop. Below is my script

$DAServers = "server1", "Server2"
$AllConnections = @()
foreach ( $a in $DAServers){

$AllConnections += get-RemoteAccessConnectionStatisticsSummary -ComputerName $a | select TotalConnections, MaxConcurrentConnections,TotalCumulativeConnections, machineName
}

$Allconnections | fl
that gives me below output.

TotalConnections : 1825
MaxConcurrentConnections : 2256
TotalCumulativeConnections : 8522

TotalConnections : 2027
MaxConcurrentConnections : 2669
TotalCumulativeConnections : 8860

But I need to add computer name also, that is not part of get-RemoteAccessConnectionSummary But I need to out computername along the other output. I trying to figure out how to add variable $a
as part of output

Any help would be greatly appreciated.
Many Thanks

Windows for business Windows Server User experience PowerShell
0 comments No comments
{count} votes

Accepted answer
  1. Anonymous
    2021-07-27T11:54:55.927+00:00

    Hi,

    You can add a calculated property using a hashtable.

    $DAServers = "server1", "Server2"  
    $AllConnections = @()  
    foreach ( $a in $DAServers){  
        $AllConnections +=  get-RemoteAccessConnectionStatisticsSummary -ComputerName $a |   
            select TotalConnections, MaxConcurrentConnections,TotalCumulativeConnections, @{n="ComputerName";e={$a}}  
    }  
    $Allconnections | fl  
    

    Best Regards,
    Ian Xue

    ============================================

    If the Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.