multi array

This topic contains 5 replies, has 3 voices, and was last updated by  Дмитрий Шлег 1 year, 9 months ago.

  • Author
  • #44308

    Hi all, can I create multi array???

    I need create in this format:


    I powershell I have 2 foreach, example

    foreach ($comp in $terminalservers){
    $sessions = Get-TSSession -ComputerName $comp -State Active  -ErrorAction SilentlyContinue
     foreach ($session in $sessions)
    //some code

    Please help generating multi array!

  • #44316

    random commandline

    I am not familiar with terminal service cmdlets, but something like this should work.

    foreach ($comp in $terminalservers){
    Get-TSSession -ComputerName $comp -State Active -ErrorAction SilentlyContinue |
    Select-Object Username,Session_Id,@{n='Server';exp={$comp}}
    • #44330

      Thx, for reply, your code is great, adds server to each session name but I need create other data array :
      One server is array key contains multiple sessions

  • #44344

    Curtis Smith

    Hey Дмитрий,
    Since you used the word key, it sounds like you are talking more about a hashtable than an array. Here is an example

    "c:\","d:\" | ForEach-Object {
        $hashtable += @{
                        $_ = (Get-ChildItem $_)


    Name                           Value                                                                                                                                                                                                                             
    ----                           -----                                                                                                                                                                                                                             
    d:\                            {DNA, Documentation, Engineering Package, ISOs...}
    c:\                            {Intel, PerfLogs, Program Files, Program Files (x86)...}
    • #44358

      I created manual solution for me

      $test =  @{"server1"=@{"0"=@{u='user1';uid='33'};"1"=@{u='user2';uid='44'};}; } 
      $test += @{"server2"=@{"0"=@{u='user1';uid='33'};}; }
      $test | ConvertTo-Json

      can I add in cycle???

  • #44510

    Solution found, thx all

    foreach ($comp in $terminalservers){
        if ((Test-Connection -computername $comp -BufferSize 16 -Count 1 -Quiet) -eq $true)
                $sessions = Get-TSSession -ComputerName $comp -State Active  -ErrorAction SilentlyContinue
                $activesession = $sessions.Count
            if($activesession -gt 0){
                $results += @{$comp=@{}}
                foreach ($session in $sessions)
                        $results.Get_Item($comp).Set_Item("$i", @{user=$($session.UserName);sid=$($session.SessionId)})
                         #$results += new-object PSObject -Property @{ Server = $session.Server.ServerName ;Session = $session.SessionId; User = $session.UserName;}
    $Json = $results | ConvertTo-Json

You must be logged in to reply to this topic.