multi array

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

Viewing 6 posts - 1 through 6 (of 6 total)
  • 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!

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

    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

    Profile photo of Curtis Smith
    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)...}

    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???


    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
Viewing 6 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic.