none
Place Result of Get_TSSession into Variable to use elsewhere

    Domanda

  • After Running

     

    Import-Module PSTerminalServices
    Get-TSSession -Filter {$_.Username -like [Environment]::Username} | Select-Object ClientName
    

    I can detect the Client Computer that a user is connecting from on a Terminal Services Server giving me the following result

    ClientName                                                                                                                                                    

    ----------                                                                                                                                                    

    ITD012

     

    How can I place that value " ITD012" into a variable ?? so some other details can be ascertained about the connecting computer? Any help appreciated 

    mercoledì 17 novembre 2010 18:32

Risposte

  • How about this, any result?

     

    Get-TSSession -UserName $env:USERNAME | Foreach-Object {$_.ClientName}


    Shay Levy [MVP]
    PowerShay.com
    PowerShell Toolbar
    • Contrassegnato come risposta Lee Haines giovedì 18 novembre 2010 11:04
    giovedì 18 novembre 2010 10:51
    Moderatore
  • Select-Object's output is an object with a subset of the properties of its input.  If you want the actual property value (as opposed to an object with that property), use the -expandProperty parameter.

    $clientName = Get-TSSession -Filter {$_.Username -like [Environment]::Username} | Select-Object -expandproperty ClientName
    # clientName is now the string "ITD012" (or an array of strings, if many results are returned)

    If you want all of the information, then you should not use Select-Object at all, just let the full object be returned

    $sessions = Get-TSSession -Filter {$_.Username -like [Environment]::Username}

    mercoledì 17 novembre 2010 19:22
  • Yes gives me ITD012 without Column Heading - as does
    Get-TSSession -UserName $env:USERNAME | Select-Object -ExpandProperty ClientName
    

    I've Assigned this to a variable and all seems to work great - Thank you for your time and effort! the working code is

    Import-Module PSTerminalServices
    $RClient = Get-TSSession -UserName $env:USERNAME | Select-Object -ExpandProperty ClientName
    Write-Host $RClient
    

     

    Any reason you would use ForEach-Object instead of SeLect_Object ?

     

     


    LHaines
    giovedì 18 novembre 2010 11:02

Tutte le risposte

  • Select-Object's output is an object with a subset of the properties of its input.  If you want the actual property value (as opposed to an object with that property), use the -expandProperty parameter.

    $clientName = Get-TSSession -Filter {$_.Username -like [Environment]::Username} | Select-Object -expandproperty ClientName
    # clientName is now the string "ITD012" (or an array of strings, if many results are returned)

    If you want all of the information, then you should not use Select-Object at all, just let the full object be returned

    $sessions = Get-TSSession -Filter {$_.Username -like [Environment]::Username}

    mercoledì 17 novembre 2010 19:22
  • By the way, you can get sessions for a specific username with the -UserName parameter:

    Get-TSSession -UserName $env:USERNAME | Select-Object -ExpandProperty ClientName
    Shay Levy [MVP]
    PowerShay.com
    PowerShell Toolbar
    giovedì 18 novembre 2010 10:07
    Moderatore
  • Thank you for the replies - I have tried both suggestions but it appears that the returned variable is empty - null

    After executing the following code nothing returned

    Import-Module PSTerminalServices
    $RemoteClient = Get-TSSession -Filter {$_.Username -like [Environment]::Username} | Select-Object -expandproperty ClientName
    $RClient = Get-TSSession -UserName $env.USERNAME | Select-Object -ExpandProperty ClientName
    Write-Host $RemoteClient
    Write-Host $RClient
    


    LHaines
    giovedì 18 novembre 2010 10:33
  • Can you see a value under the ClientName header when you execute the code below (it could be empty)?
    You also have a typo in '$env.USERNAME', replace the dot with a colon:

    Get-TSSession -UserName $env:USERNAME
    Shay Levy [MVP]
    PowerShay.com
    PowerShell Toolbar
    giovedì 18 novembre 2010 10:42
    Moderatore
  • Thank you the prompt reply - 

    Yes I can see the client name 'ITD012' under the ClientName header


    LHaines
    giovedì 18 novembre 2010 10:47
  • How about this, any result?

     

    Get-TSSession -UserName $env:USERNAME | Foreach-Object {$_.ClientName}


    Shay Levy [MVP]
    PowerShay.com
    PowerShell Toolbar
    • Contrassegnato come risposta Lee Haines giovedì 18 novembre 2010 11:04
    giovedì 18 novembre 2010 10:51
    Moderatore
  • Yes gives me ITD012 without Column Heading - as does
    Get-TSSession -UserName $env:USERNAME | Select-Object -ExpandProperty ClientName
    

    I've Assigned this to a variable and all seems to work great - Thank you for your time and effort! the working code is

    Import-Module PSTerminalServices
    $RClient = Get-TSSession -UserName $env:USERNAME | Select-Object -ExpandProperty ClientName
    Write-Host $RClient
    

     

    Any reason you would use ForEach-Object instead of SeLect_Object ?

     

     


    LHaines
    giovedì 18 novembre 2010 11:02
  • To get just the value(s), in PowerShell V1, we used the Foreach-Object. In V2 we can do the same with Select-Object -ExpandProperty.
    Shay Levy [MVP]
    PowerShay.com
    PowerShell Toolbar
    giovedì 18 novembre 2010 11:05
    Moderatore
  • This is a great tool.

     

    I'm trying to figure out how to parse the Server from the list to use in other areas.  Is this possible?  When I try now I get "Cassia.Impl.TerminalServer"

     

    Any help you could provide would be greatly appreciated.

    mercoledì 5 gennaio 2011 21:42
  • $session.Server.ServerName
    Shay Levy [MVP]
    PowerShay.com
    PowerShell Toolbar
    giovedì 6 gennaio 2011 08:27
    Moderatore
  • Hello Shay,

    I'm new to Powershell and i'm using your script to get a list of disconnected sessions in a given set of servers(located on a txt file) i'm stuck in trying to get the results of server name for the below script. Any ideas?

    $FilePath = "D:\RemoteSessionsList"
    $OutFile = Join-Path -path $FilePath -childPath ("TSSessions_" + (get-date).toString('yyyyMMdd_hhmmtt') + ".log")

    @(foreach ($svr in get-content "D:\RemoteSessionsList\ServerSessions.txt")
    {
        $strSysinfo = Get-TSSession -computername $svr | Where {$_.State -eq "Disconnected"}
        $strSysinfo | select Server, SessionID, UserName, State, ClientName, LoginTime, CurrentTime
      })  | export-csv -noType $OutFile

      $end = get-date  

    martedì 26 giugno 2012 23:24
  • Hi Lee

    The server name exist on a property of the Server property, you can get it with a calculated property:

    Get-Content D:\RemoteSessionsList\ServerSessions.txt | Foreach-Object{ Get-TSSession -ComputerName $_ -State Connected | select @{n='ComputerName';e={$_.Server.ServerName}}, SessionID, UserName, State, ClientName, LoginTime, CurrentTime
    } | Export-Csv -NoType $OutFile




    Shay Levy [MVP]
    PowerShay.com
    PowerShell Toolbar


    mercoledì 27 giugno 2012 15:47
    Moderatore
  • Gr8! Got it now. It´s working as i expected.

    Again, thanks a lot for this great script!!

    giovedì 28 giugno 2012 00:47