Foreach array/loop help

Welcome Forums General PowerShell Q&A Foreach array/loop help

Viewing 6 reply threads
  • Author
    Posts
    • #212310
      Participant
      Topics: 1
      Replies: 4
      Points: 0
      Rank: Member

      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 🙂

      • This topic was modified 2 months, 1 week ago by Chris_94.
    • #212325
      Participant
      Topics: 0
      Replies: 3
      Points: 5
      Rank: Member

      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…

    • #212337
      Participant
      Topics: 1
      Replies: 4
      Points: 0
      Rank: Member

      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

    • #212346
      Participant
      Topics: 1
      Replies: 4
      Points: 0
      Rank: Member

      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
      
    • #212358
      Participant
      Topics: 1
      Replies: 4
      Points: 0
      Rank: Member

      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?

    • #212505
      Participant
      Topics: 12
      Replies: 524
      Points: 1,220
      Helping Hand
      Rank: Community Hero

      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 ', ')
      
    • #212538
      Participant
      Topics: 1
      Replies: 4
      Points: 0
      Rank: Member

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

Viewing 6 reply threads
  • You must be logged in to reply to this topic.