1

I have a small script that will help users in our environment shadow the sessions from other users in the facilities (I work at the corporate office, corporate users need the ability to shadow other user's sessions along different sites)

So I've got this:

$servers = "server1, server2, server3, server4, server5"

#search for user in servers farm
foreach ($server in $servers) { 

$results = & qwinsta.exe $userid /server:$server 

    if ($results -ne $null) {

        write-host "User found in $server"

        $serverfound = $server

        write-host $results[1]

        $sessionid = $results[1] | Where-Object { $_ -is 0..99}

        write-host "Session ID is: $sessionid"
    }
}

However, I can't get it to grab the session id from the result, this is the output of the script:

Please wait while we search for x2adm on the RDSfarm Servers

User found in server5

rdp-tcp#0 x2adm 2 Active

Session ID is:

I am trying to simply grab the number in the output and assign it to $sessionid so that I can fully automate the shadowing process.

Any help will be appreciated. Thanks in advance.

Kara
  • 6,115
  • 16
  • 50
  • 57
Unsecur3d
  • 13
  • 3
  • There is a good answer here to help you : http://stackoverflow.com/questions/23445175/qwinsta-serversomesrv-equivalent-in-powershell – sodawillow Jun 30 '16 at 15:01
  • 1
    I think your problem is with $servers = "server1, server2, server3, server4, server5". Should be $servers = "server1", "server2", "server3", "server4", "server5" so you get an array with 5 elements. – Dan Loughney Jun 30 '16 at 15:20
  • I agree with Dan, you just passed it a string and not 5 individual items. – Lord Helmet Jun 30 '16 at 16:10

2 Answers2

2

I would recommend you use ConvertFrom-String instead. It's awesome for parsing data like this.

ConvertFrom-String $results[1]

P1 : 
P2 : console
P3 : Stephen
P4 : 1
P5 : Active
P6 : 

So our $SessionID value would be found in p4 (P# is just an arbitrary name that this cmdlet gives to the strings it finds).

The full command would be

$sessionID = convertfrom-string $results[1] | select -expand P4

This will work with your current code with no further modifications.

FoxDeploy
  • 12,569
  • 2
  • 33
  • 48
  • I forgot about ConvertFrom-String. So used to using ConvertFrom-StringData – Matt Jun 30 '16 at 15:42
  • I am sorry for my delay work got really busy this past few weeks, this solution worked this is exactly what i needed to finish the automation. I greatly appreciate it. – Unsecur3d Jul 22 '16 at 14:54
1

Check the data type of $servers and you will see that it is a string and not a string array which is how you are treating it.

PS C:\Users\matt> $servers = "server1, server2, server3, server4, server5"
PS C:\Users\matt> $servers.GetType().FullName

System.String

You can read more about the basic concepts of arrays from about_arrays You need to define it like this.

$servers = "server1", "server2", "server3", "server4", "server5" 

As far as dealing with output from qwinsta I have an answer that converts data from that into a PowerShell object. Might not need to be that extreme since you are not using the exact same command that the Op of the linked question was. Point being is that ConvertFrom-StringData is a powerful cmdlet.

Community
  • 1
  • 1
Matt
  • 45,022
  • 8
  • 78
  • 119
  • It returns an array when the username starts with a number, this is the last problem i am having with it i can't seem to be able to convert it to a string, i tried: [string[]]$results = & qwinsta.exe "$userid" /server:$server [string]$results = & qwinsta.exe "$userid" /server:$server $results -join ' ' And the type either comes back array or object, how can i make it return a string regardless ? – Unsecur3d Jul 22 '16 at 15:45