multi array

This topic contains 5 replies, has 3 voices, and was last updated by Profile photo of Дмитрий Шлег Дмитрий Шлег 5 months, 2 weeks ago.

  • Author
    Posts
  • #44308

    Hi all, can I create multi array???

    I need create in this format:

    
    [server1][array(username,session_id),array(username,session_id),array(username,session_id),array(username,session_id)]
    
    
    [server2][array(username,session_id),array(username,session_id),array(username,session_id),array(username,session_id)]
    
    

    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
    Profile photo of random commandline
    random commandline
    Participant

    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
    Profile photo of Curtis Smith
    Curtis Smith
    Participant

    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 $_)
                     }
    }
    $hashtable
    

    Results:

    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

    $results=@{}
    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=@{}}
    
                        [int]$i=0 
                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;}
    
                        $i++;
                        }
            }
    
        }
    }
    
    $Json = $results | ConvertTo-Json
    $Json
    

You must be logged in to reply to this topic.