Author Posts

June 22, 2016 at 9:59 am

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!

June 22, 2016 at 12:44 pm

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

June 22, 2016 at 1:44 pm

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

June 22, 2016 at 2:28 pm

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)...}

June 22, 2016 at 3:33 pm

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

June 23, 2016 at 7:08 am

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