question

SaeedAhmad-2252 avatar image
0 Votes"
SaeedAhmad-2252 asked SaeedAhmad-2252 commented

Adding variable cotaining computername in foreach loop in powershell

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-server-powershell
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

IanXue-MSFT avatar image
0 Votes"
IanXue-MSFT answered SaeedAhmad-2252 commented

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.




· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

That worked like a charm, you are such a genius - thank you.

0 Votes 0 ·