Foreach array/loop help

Hi All,

Apologies if the title is a bit confusing, not sure which one it is.

I’m having issues with output on a Get-DAMultiSite script I’m writing. It part works, the first part of the script before creating the array does the output I want, but if I want a count of users, it only outputs the users on the last server, not all servers. I’m getting weird results

$UserSummary = @()

$Servers = Get-DAMultiSite | Select -ExpandProperty DAEntryPoints | Select -ExpandProperty Servers | Sort

Foreach ($Server in $Servers)

{$Users = Get-RemoteAccessConnectionStatistics -ComputerName $Server | Where { $.HostName -like “ABC*” } | Select @{ Label = “Server”; Expression = { ( $Server ).ToString() }},UserName,HostName,ConnectionStartTime,@{ Label = “ConnectionDuration”; Expression = { ( New-TimeSpan -Seconds $.ConnectionDuration ).ToString() }} | Sort Server,UserName }

Foreach ($User in $Users) {

$UserObject = New-Object PSObject

$UserObject | Add-Member -MemberType NoteProperty -Name “Server” -Value $User.Server
$UserObject | Add-Member -MemberType NoteProperty -Name “UserName” -Value $User.Username
$UserObject | Add-Member -MemberType NoteProperty -Name “HostName” -Value $Users.HostName
$UserObject | Add-Member -MemberType NoteProperty -Name “Connection Start Time” -Value $User.ConnectionStartTime
$UserObject | Add-Member -MemberType NoteProperty -Name “Connection Duration” -Value $User.ConnectionDuration

$UserSummary += $UserObject

}

$UserSummary | ft

 

Could do with some pointers to gather the whole lot.

 

This bit delivers what I want to see,

$Servers = Get-DAMultiSite | Select -ExpandProperty DAEntryPoints | Select -ExpandProperty Servers | Sort

Foreach ($Server in $Servers)

{$Users = Get-RemoteAccessConnectionStatistics -ComputerName $Server | Where { $.HostName -like “ABC*” } | Select @{ Label = “Server”; Expression = { ( $Server ).ToString() }},UserName,HostName,ConnectionStartTime,@{ Label = “ConnectionDuration”; Expression = { ( New-TimeSpan -Seconds $.ConnectionDuration ).ToString() }} | Sort Server,UserName }

but I can’t get it all in a variable, say $UserSummary.Count and only shows for the last server queried.

 

Thank you all :slight_smile:

You have 2 foreach loops running sequentially - first you are going through the list of servers, populating $users for each one. After that foreach is complete, $users contains only the users from the last server in the set. This is why the second foreach loop only contains the users from the last server you ran against. Embed the users foreach loop inside the first foreach, so that it collects the information for the users from each server, and you should have the complete list you are trying to obtain.

I hope that makes sense. Let me know if you need additional help with how to do that…

Hi David,

Thank you for the reply. It’s still not working as I want, but I’m not sure I’ve done as you suggested correctly

$UserSummary = @()

$Servers = Get-DAMultiSite | Select -ExpandProperty DAEntryPoints | Select -ExpandProperty Servers | Sort

Foreach ($Server in $Servers)

{$Users = Get-RemoteAccessConnectionStatistics -ComputerName $Server | Where { $_.HostName -like “ABC*” } | Select @{ Label = “Server”; Expression = { ( $Server ).ToString() }},UserName,HostName,ConnectionStartTime,@{ Label = “ConnectionDuration”; Expression = { ( New-TimeSpan -Seconds $_.ConnectionDuration ).ToString() }} | Sort Server,UserName

$UserObject = New-Object PSObject

$UserObject | Add-Member -MemberType NoteProperty -Name “Server” -Value $User.Server
$UserObject | Add-Member -MemberType NoteProperty -Name “UserName” -Value $User.Username
$UserObject | Add-Member -MemberType NoteProperty -Name “HostName” -Value $User.HostName
$UserObject | Add-Member -MemberType NoteProperty -Name “Connection Start Time” -Value $User.ConnectionStartTime
$UserObject | Add-Member -MemberType NoteProperty -Name “Connection Duration” -Value $User.ConnectionDuration

$UserSummary += $UserObject

}

$UserSummary | ft

It’s not displaying as I’d like properly or

UserName : { domain\user1, domain\user2 etc. }
Server : { server1.domain, server2.domain }
HostName : { hostname1.domain, hostname2.domain }
ConnectionStartTime : {dd/mm/yy h:mm:ss }

So on and so forth, and it does like that for each server, of which we have 7, so my $UserSummary.Count equals 7, not 100+ like I’d expect

Think I figured it now, thank you. I could have swore I tried this earlier, must have been getting something completely off, yet so simple. Maybe I used the wrong variable of $users

 

$UserSummary = @()
$Servers = Get-DAMultiSite | Select -ExpandProperty DAEntryPoints | Select -ExpandProperty Servers | Sort

Foreach ($Server in $Servers)

{$Users = Get-RemoteAccessConnectionStatistics -ComputerName $Server | Where { $_.HostName -like "ABC*" } | Select @{ Label = "Server"; Expression = { ( $Server ).ToString() }},UserName,HostName,ConnectionStartTime,@{ Label = "ConnectionDuration"; Expression = { ( New-TimeSpan -Seconds $_.ConnectionDuration ).ToString() }} | Sort Server,UserName

ForEach ($User in $Users) {

$UserObject = New-Object PSObject

$UserObject | Add-Member -MemberType NoteProperty -Name "Server" -Value $User.Server
$UserObject | Add-Member -MemberType NoteProperty -Name "UserName" -Value $User.Username
$UserObject | Add-Member -MemberType NoteProperty -Name "HostName" -Value $User.HostName
$UserObject | Add-Member -MemberType NoteProperty -Name "Connection Start Time" -Value $User.ConnectionStartTime
$UserObject | Add-Member -MemberType NoteProperty -Name "Connection Duration" -Value $User.ConnectionDuration

$UserSummary += $UserObject

}
}

$UserSummary

Sorry just one more, and hopefully easy, but on export to csv the username is showing as “System.String[]” instead of domain\username. Do you know how I fix that?

This error is telling you that you have a multi-value property. To show that in CSV you combine them as in:
Replace

$UserObject | Add-Member -MemberType NoteProperty -Name "UserName" -Value $User.Username

with

$UserObject | Add-Member -MemberType NoteProperty -Name "UserName" -Value ($User.Username -join ', ')

Thank you Sam, worked a charm. Thought it would be something simple, yet I didn’t know haha. Cheers