none
PS: filter on unique object in hashtable RRS feed

  • Question

  •  Hi,

    I got this $overview variable in which I have stored the outcome of a get-rdusersession.
     This is $overview:

     CollectionName      UserName          HostServer

     Collection1          user1            Server1
     Collection1          user2            Server2   
     Collection1          user3            Server4

     I now like to see all users per server and count them. I would need to filter on an unique Hostserver but how can I do this?

     This doesn't work:
      $overview |  Where-Object  Hostserver -unique,Username  

      Please advise.
      J.

         

    Jan Hoedt

    Wednesday, May 11, 2016 12:58 PM

Answers

All replies

  • Hi Jan,

    you'll want to experiment with Group-Object:

    $overview | Group-Object HostServer | %{
        $_ | Add-Member -Name "Users" -MemberType Noteproperty -Value ($_.Group | Select -expand Username -unique)
        $_
    } | FT * -AutoSize

    Cheers,
    Fred

    Note: I haven't tried running this, may work, may need some adjustments


    There's no place like 127.0.0.1


    • Edited by FWN Wednesday, May 11, 2016 1:04 PM
    Wednesday, May 11, 2016 1:04 PM
  • You say hashtable, but I'm assuming you actually have an object.

    $hashes = @(
        @{CollectionName='Collection1';UserName='user1';HostServer='Server1'}
        @{CollectionName='Collection1';UserName='user2';HostServer='Server2'}
        @{CollectionName='Collection1';UserName='user3';HostServer='Server4'}
    )
    
    $obj = $hashes | % { New-Object PsObject -Property $_ }
    
    $obj | Where { $_.HostServer -eq 'Server1' }


    Wednesday, May 11, 2016 1:07 PM
  • This works for me

     $overview | Group-Object hostserver | Select-Object name,count

    it lists is as following

    Name                    Count
    ----                    -----
    server1     4
    server2   6


    Jan Hoedt

    Wednesday, May 11, 2016 2:02 PM
  • Hi Jan,

    so long as no user can be listed for the same server multiple times (which shouldn't be much of an issue with rdsessions), this will indeed work just fine.

    Cheers,
    Fred


    There's no place like 127.0.0.1

    Wednesday, May 11, 2016 2:34 PM
  • $overview | Format-Table -GroupBy HostServer


    \_(ツ)_/

    • Marked as answer by janhoedt Thursday, May 12, 2016 8:12 AM
    Wednesday, May 11, 2016 4:09 PM
  • The simpeler the better, great!

    Jan Hoedt

    Thursday, May 12, 2016 8:12 AM
  • An extra challenge would be to count the users on each server.

    Then also the usernames are samaccountnames. I have a function which resolves them to surname givenname but would like to select these per server and add a column with fullname + the membership of a specific group they belong to (we have different groups of users which work on rds, it would be nice how many users of which group are working on which server).

    I'll see where I get with this but any advise on howto approach is welcome.


    Jan Hoedt

    Thursday, May 12, 2016 8:16 AM
  • Regarding my last comment: I am almost there. I'll create another post for this since I have a particular question.

    Jan Hoedt

    Thursday, May 12, 2016 11:41 AM