locked
Powershell: add data into array RRS feed

  • Question

  • Hi,

    I would like to insert data into an array.

    I collect info from RDS-servers with active user sessions with Get-RDUsersession ...
    However, I also like to show servers which have no user sessions with Get-RDServer ... 

    This is what I have so far

    $ConnectionBroker = "ourconnectionbroker"
    $AllRDSServers = $(Get-RDServer -ConnectionBroker $ConnectionBroker).server #Gets all RDS servers
    $overview = Get-RDUserSession -ConnectionBroker $ConnectionBroker -CollectionName * #Gets all RDS servers with active user connections
    $overviewuniquehosts =  $($overview | Select-Object -Unique hostserver).hostserver #Filter out duplicates
    $RDSHostsNoUserSessions = (Compare-Object -ReferenceObject $AllRDSServers -DifferenceObject $overviewuniquehosts).inputobject #Get only the hosts on which no user sessions are, I like to add them to $Overview
    


    ---
    These are the contents of the variables:

    $Overview 

    CollectionName    DomainName        UserName          HostServer                                          
    Collection1       OurDomain          user1            server1.domain.com 

    $RDSHostsNoUserSessions
    server6.domain.com
    server4.domain.com

    How do I add these servers $RDSHostsNoUserSessions to $Overview?

    Please advise

    Note: I have this customobject but howto add them to the array?

    $Result = @()
    $RDSHostsNoUserSessions | ForEach-Object {
      $Result += [PSCustomObject] @{
    
        HostServer =  $_
      }
    }


                                                                   

    Jan Hoedt


    • Edited by janhoedt Wednesday, March 22, 2017 5:54 PM update
    Wednesday, March 22, 2017 5:53 PM

Answers

  • $array-@{}
    $array+=$newobject


    \_(ツ)_/

    • Marked as answer by janhoedt Thursday, March 23, 2017 8:09 AM
    Wednesday, March 22, 2017 6:08 PM
  • Note: $Overview is an

    Microsoft.RemoteDesktopServices.Management.RDUserSession object

    Should I convert that to a hashtable?


    Jan Hoedt

    • Marked as answer by janhoedt Thursday, March 23, 2017 8:09 AM
    Wednesday, March 22, 2017 7:59 PM

All replies

  • $array-@{}
    $array+=$newobject


    \_(ツ)_/

    • Marked as answer by janhoedt Thursday, March 23, 2017 8:09 AM
    Wednesday, March 22, 2017 6:08 PM
  • Then it adds the items (servers) just at the bottom, not in the columns for hostservers

    Jan Hoedt

    Wednesday, March 22, 2017 7:48 PM
  • Note: $Overview is an

    Microsoft.RemoteDesktopServices.Management.RDUserSession object

    Should I convert that to a hashtable?


    Jan Hoedt

    • Marked as answer by janhoedt Thursday, March 23, 2017 8:09 AM
    Wednesday, March 22, 2017 7:59 PM
  • So yes I had to.

    Jan Hoedt

    Wednesday, March 22, 2017 8:03 PM
  • You cannot insert into an array.  Use an arraylist if you need to insert.


    \_(ツ)_/

    Wednesday, March 22, 2017 8:10 PM